Android通知集不起作用

时间:2018-08-20 21:08:37

标签: android notifications timestamp

我正在尝试更改为Android通知显示的时间戳,但我总是获取当前时间。现在,我正在创建这样的通知:

NotificationCompat.Builder notificationBuilder =
    new NotificationCompat.Builder(context.getApplicationContext(), CHANNEL_ID)
...
notificationBuilder.setShowWhen(true)
notificationBuilder.setWhen(exactNotificationTime);

我认为这已经足够了,但是我仍在显示当前时间;我的手机是具有Android 6.0(API级别23)的华为。我的项目配置了minSdkVersion = 23和targetSdkVersion = 27。

1 个答案:

答案 0 :(得分:0)

您需要在setWhen()中添加以毫秒为单位的时间

notificationBuilder.setWhen(System.currentTimeMillis())

而不是currentTimeMillis给出您想要的时间。

如果要在特定时间显示通知,则可能需要使用警报管理器,如下所示。

首先用您要显示通知的时间创建警报管理器

Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.HOUR_OF_DAY, 12);
     calendar.set(Calendar.MINUTE, 0);
     calendar.set(Calendar.SECOND, 0);
Intent notifyIntent = new Intent(getApplicationContext(),showNotification.class);
       notifyIntent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(etApplicationContext(),0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
             alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  calendar.getTimeInMillis(),1000 * 60 * 60 * 24, pendingIntent);

然后进入showNotification类

public class showNotification extends BroadcastReceiver {

public showNotification() {

}

@Override
public void onReceive(Context context, Intent intent) {
   sendNotification(context);
}

private void sendNotification(Context context) {


    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent intent = new Intent(context, SecondActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "101";

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);

        //Configure Notification Channel
        notificationChannel.setDescription("Game Notifications");
        notificationChannel.enableLights(true);
        notificationChannel.setVibrationPattern(new long[]{200});
        notificationChannel.enableVibration(false);

        notificationManager.createNotificationChannel(notificationChannel);
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle(title.get("Title of notification"))
            .setContentText(subText.get("Sub text of notification"))
            .setAutoCancel(true)
            .setSound(defaultSound)
            .setContentIntent(pendingIntent)
            .setWhen(System.currentTimeMillis())
            .setPriority(Notification.PRIORITY_MAX);


    notificationManager.notify(1, notificationBuilder.build());

  }
}