多个通知未显示在android中

时间:2015-11-20 11:30:21

标签: android notifications

在我的Android应用程序中,我愿意在特定的日子显示多个通知,为此我使用了警报管理器和广播接收器问题,当我使用警报时,它工作正常,但是当我添加通知构建器时显示通知不起作用

这是我的mainActivity

  public static final String ACTION_ONE = "Hello, Test Message 1";
public static final String ACTION_TWO = "Hello, Test Message 2";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());

    Intent myIntent1 = new Intent(this, AlarmBroadCustReciver.class);
    myIntent1.setAction(ACTION_ONE);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 1253, myIntent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    // Set the time for first alarm here
    cal.set(2015, 10, 20, 15, 55);
    alarmManager1.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent1);

    Intent myIntent2 = new Intent(this, AlarmBroadCustReciver.class);
    myIntent2.setAction(ACTION_TWO);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1263, myIntent2,
            PendingIntent.FLAG_UPDATE_CURRENT);
    // Set the time for second alarm here
    cal.set(2015, 10, 20, 15, 56);
    alarmManager1.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent2);
    // In this way set time for all the rest of the alarms

这是BroadCastReceiver

public class AlarmBroadCustReciver extends BroadcastReceiver {

public static final String ACTION_ONE = "Hello, welcome to the Server1";
public static final String ACTION_TWO = "Hello, welcome to the Server2";

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
    builder.setContentTitle(context.getString(R.string.app_name));
    if (intent.getAction().equalsIgnoreCase(ACTION_ONE)) {
        builder.setContentText("Alarm one");
    } else {
        builder.setContentText("Alarm two");
    }
    Notification notification = builder.build();
    int notificationID = 0;
    notificationManager.notify(notificationID, notification);

3 个答案:

答案 0 :(得分:0)

您需要为每个通知传递不同的通知ID。如果您传递相同的ID(例如,在您的情况下为0),则将使用新数据更新现有通知。 更改通知ID:例如,有一个变量并递增它。 notificationid ++

答案 1 :(得分:0)

我有一个类似的问题,我正在使用不同的ID创建多个通知,但是,当我点击一个,只有第一个打开特定的scree时,所有顺序通知都被忽略(点击它们没有&t; t做任何事情,他们只是被解雇了)。然后我试着这样做:

intent.setAction(context.getPackageName() + "." + notificationId);

这意味着每个唯一通知也带有自己唯一的Intent,在这种情况下,Intents不会被忽略,并且每个都打开了所需的屏幕。我必须注意我的意图是相同的(例如NoteDetails.class),所以我想我必须以某种方式将它们分开......我很高兴它仍然有效。

答案 2 :(得分:0)

Box's answer对我来说是正确的方向。

我只需要在传递给AlarmManager的PendingIntent中更改requestCode,因此它的值不相同。

旧代码:

PendingIntent pI = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

新代码:

PendingIntent pI = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);