在getNotification()上指定notificationID(ParsePushBroadcastReceiver)

时间:2014-12-02 16:27:44

标签: android parse-platform push android-notifications

我正在为我的应用程序继承ParsePushBroadcastReceiver并覆盖getNotification(),一切正常但每次我都会创建一个新的通知,我想将相同的notificationID传递给NotificationManager以避免这种情况。

我试过打电话:

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

mNotificationManager.notify(Constants.NOTIFICATION_ID, mBuilder.build());

在getNotification()上返回自定义通知之前,但不起作用。任何想法如何实现?

感谢。

1 个答案:

答案 0 :(得分:1)

这就是我解决同样问题的方法。您只需要控制通知所采用的路径。传入来自Notification的{​​{1}}对象或您构建到NotificationManager实例的对象,以实现您想要的效果。只要您对作为第一个参数传入的id使用相同的值,它就会用新的通知替换任何现有通知。

getNotification()

在上面的示例中,我将public class MyParsePushBroadcastReceiver extends ParsePushBroadcastReceiver { NotificationManager mNotificationManager; @Override public void onReceive(Context context, Intent intent) { mNotificationManager = (NotificationManager)context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); super.onReceive(context, intent); ... } @Override protected Notification getNotification(Context context, Intent intent) { Notification n = super.getNotification(context, intent); int type = intent.getExtras().getInt("NOTIFICATION_TYPE"); mNotificationManager.notify(type, n); return null; } } 上的null返回并自行处理发送至getNotification。请注意,我从NotificationManager抓取Notification对象,但您可以使用“通知”构建器自由构建自己的super.getNotification()对象,就像您已经在做的那样。

请记住,您可以从通过推送通知发送的Intent包中提取任何数据,以便您可以确定通知的类型,以便您可以相应地更改常量值。这使您能够在通知中定位特定行。在我的应用程序中,我有3种不同类型的通知可能会进来,所以这样我最多只能在通知中占用3个不同的行。如果您只需要1个通知,那么您不必担心这一点,并且您可以继续使用您的常量值。