简体   繁体   English

Android多个通知BroadcastReceiver

[英]Android Multiple notifications BroadcastReceiver

I am trying to implement multiple notifications for each reminder I have in my app. 我正在尝试为我的应用程序中的每个提醒实施多个通知。 I found some leads around here that it could be achieved using unique id per each notification but it is not working I get only one notification which is overwritten by the last one set. 我在这里发现了一些潜在客户,可以使用每个通知的唯一ID来实现此目的,但是它不起作用,我仅收到一个通知,该通知被最后一组覆盖。 Also I would like know how is it possible to keep the notification intact after device is rebooted please. 另外,我想知道重启设备后如何保持通知的完整性。 Here is my code 这是我的代码

 //call notification activation Intent
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

AlarmReceiver class AlarmReceiver类

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        //creating unique id for each specific notification
        int ID = (int) ((new Date().getTime()/1000L) % Integer.MAX_VALUE);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notification = new Intent(context,MainActivity.class);
        notification.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pi = PendingIntent.getActivity(context,0,notification,0);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mynotification = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_date_range_black_48dp)
                .setContentTitle("note taker")
                .setContentText("you have a reminder about a note")
                .setAutoCancel(true)
                .setContentIntent(pi)
                .setSound(defaultSoundUri)
                .setVibrate(new long[]{1000,1000,1000,1000,1000});

        notificationManager.notify(ID,mynotification.build());
    }
}

I found some leads around here that it could be achieved using unique id per each notification but it is not working i get only one notification which is overwritten by the last one set 我在这里发现了一些潜在客户,可以使用每个通知的唯一ID来实现此目的,但是它不起作用,我只收到一个通知,该通知被最后一覆盖

I think the problem lies not in your notification, but in your AlarmManager . 我认为问题不在于通知,而在于AlarmManager Try using different ID for each PendingIntent , and set the flag to PendingIntent.FLAG_ONE_SHOT . 尝试为每个PendingIntent使用不同的ID ,并将标志设置为PendingIntent.FLAG_ONE_SHOT

Your calling intent should look like this: 您的呼叫意图应如下所示:

//call notification activation Intent
Intent intent = new Intent(getBaseContext(), AlarmReciever.class);
int RQS_1 = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

尝试提供其他请求代码和PendingIntent.FLAG_UPDATE_CURRENT作为Pending Intent的标志,并进行如下检查:

PendingIntent pi = PendingIntent.getActivity(context, new Random().nextInt(1000000), notification, PendingIntent.FLAG_UPDATE_CURRENT);

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

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