繁体   English   中英

android 中未显示通知

[英]Notifications not showing in android

我有多个通知但是,如果时间到了,其他的都没有出现,任何想法我该如何解决这个问题每个通知都有唯一的数据尝试过这个但没有工作多个通知没有显示在 android

 //Schedule alarm notification
private void scheduleNotification(Notification notification, long delay) {
    Intent notificationIntent = new Intent(getContext(), MyNotificationPublisher.class);
    notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION, notification);
    notificationIntent.putExtra(MyNotificationPublisher.NOTIF_CONTENT, title);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    assert alarmManager != null;
    alarmManager.set(AlarmManager.RTC_WAKEUP, delay, pendingIntent);
    Log.d(TAG, "scheduleNotification: Notification set successfully!");
}

//Build notification
private Notification getNotification(String content) {
    //on notification click open MainActivity
    Intent intent = new Intent(getContext(), SchedulerFragment.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), default_notification_channel_id);
    builder.setContentTitle("ToDo Reminder");
    builder.setContentText(content);
    builder.setContentIntent(pendingIntent);
    builder.setAutoCancel(true);
    builder.setSmallIcon(R.drawable.ic_stat_name);
    builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);
    builder.setChannelId(NOTIFICATION_CHANNEL_ID);
    builder.setPriority(NotificationCompat.PRIORITY_HIGH);
    return builder.build();
}

O 通知发布者

 public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(SchedulerFragment.NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        assert notificationManager != null;
        notificationManager.createNotificationChannel(notificationChannel);
    }
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    String con = intent.getStringExtra(NOTIF_CONTENT);
    assert notificationManager != null;
    notificationManager.notify(id, notification);

你可以试试下面的代码,这个方法是用来生成通知的。 如果不同的通知有不同的channelId ,可以同时生成多个通知。

    private void initNotification(Context context,String channelId,String channelName,String title,String content){
        Intent activityIntent = new Intent(context,MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(context,0,activityIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationChannel channel = new NotificationChannel(channelId,
                channelName, NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);
        Notification notification = new NotificationCompat.Builder(context,channelId)
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pi)
                .build();
        manager.notify(Integer.parseInt(channelId),notification);
    }

确保String channelID的值不同以生成多个通知。

在此处输入图像描述

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM