【问题标题】:Two parallel notifications from one app (android)来自一个应用程序的两个并行通知 (android)
【发布时间】:2016-08-19 19:57:59
【问题描述】:

我想编写一个发送两种类型通知的应用程序。用户应该看到它们。

到目前为止,通知会相互更新,即使我更改了 pendingIntents 的标志。

这是我的代码:

Calendar calendar = Calendar.getInstance();

Intent intent = new Intent(this, Push.class);
Intent intent2 = new Intent(this, Push2.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+10, pendingIntent);
alarmManager2.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+10, pendingIntent2);

【问题讨论】:

标签: java android push-notification alarmmanager android-notifications


【解决方案1】:

创建 2 个不同的通知构建器对象,

例子

第一个通知对象

Notification.Builder builder = new Notification.Builder(context);

builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.some_img)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))            
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.your_notif_title))
            .setContentText(res.getString(R.string.your_notif_text));
Notification n1 = builder.build();

第二个通知对象

Notification.Builder builder2 = new Notification.Builder(context);

builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.some_img)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))            
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.your_notif_title))
            .setContentText(res.getString(R.string.your_notif_text));
Notification n2 = builder2.build();

NotificationManager nm = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);

使用通知管理器显示通知

nm.notify(YOUR_NOTIF_ID, n1);
nm.notify(YOUR_NOTIF_ID_2, n2);

请注意,参考代码取自this answer

【讨论】:

  • @Incam 如果有帮助,请接受答案。
猜你喜欢
  • 2017-06-26
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多