如何在不影响待处理意图及其有效载荷的情况下更新通知?

3

我正在使用以下代码创建通知:

Intent intent = new Intent(this, GetStockQuote.class);
            intent.putExtra("abc", abcObject);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

            /* Build the notification */
            Notification notification = new Notification.Builder(this)
                                     .setContentTitle(abcObject.getCode())
                                     .setContentText(abcObject.getText())
                                     .setAutoCancel(false)
                                     .setSmallIcon(R.drawable.ic_launcher)
                                     .setContentIntent(pIntent).build();
 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(notificationID,notification);
            Log.i(TAG,"notification.notified");

正如您所看到的,附加在通知上的PendingIntent携带着有效载荷。它是一个自定义类的对象。

现在我要在服务中更新通知。我知道如果要更新通知(而不是创建新通知),必须指定相同的notificationID,我正在这样做。

以下是用于更新上面创建的通知的服务中的代码:

Intent intent = new Intent(this, GetStockQuote.class);
                PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);   
 Notification notification=new Notification.Builder(this)
                                .setContentTitle(newTitle)
                                .setContentText(newBody)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setContentIntent(pIntent)
                                .build();

                /*Get instance of Notification Manager and show the notification*/
                        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                        notificationManager.notify(notificationID,notification);

该代码更新现有的通知内容,但PendingIntent不包含有效载荷。

在服务中,我无法访问有效载荷。因此,我想仅使用其新的textContent更新通知服务,而不影响在创建期间设置的有效载荷。

现在的问题是,我有许多这样的通知。它们每个都有一个唯一的有效负载,但是Intent中的目标类保持不变。

是否有一种方法可以在更新通知时保留有效载荷?


当您启动服务时,可以将对象传递给服务,以便在服务中访问它。 - Misagh Emamverdi
@MisaghEmamverdi 不可以,该服务只启动一次,并更新所有活动通知。 - Ashwin
服务应该传递不同的额外对象到待定意图吗? - Misagh Emamverdi
@MisaghEmamverdi 该服务每15分钟运行一次。当它运行时,会更新所有活动的通知。 - Ashwin
那么当服务运行时,如果有5个通知处于活动状态,它将更新它们并将5个不同的对象传递给它们。我说得对吗? - Misagh Emamverdi
2个回答

6
您正在为所有通知设置具有相同ID的挂起意图。
使用以下内容:
PendingIntent pIntent = PendingIntent.getActivity(this, notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

替代

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 

是的,太棒了。它起作用了。那么这个第二个参数——requestCode的用途是什么?在Android开发者文档中没有给出太多信息。 - Ashwin

0

然而我不确定这是唯一的方法或最好的方法,但它可能会对你有所帮助:

首先为您应该传递给意图的类定义解析器方法。您需要这两种方法:

public static String getStringObject(YourObj obj) // return your fileds as string
public static YourObj parse(String input) // parse the string and create an object

然后,每次您想要显示新通知时,请添加以下内容:

SharedPreferences sp = getSharedPreferences("pref",MODE_PRIVATE);
Set<String> notifications = sp.getStringSet("nots", null);
if(notifications==null){
    notifications = new HashSet<String>();
}
notifications.add(id + "|" + YourObject.getStringObject(abcObject));
sp.edit().putStringSet("nots", notifications).commit();

在您的服务中,您将通过调用(例如第一个活动通知)来访问您的对象:
YourObj obj = YourObj.parse(new ArrayList<String>(sp.getStringSet("nots", null)).get(0).split("|")[1]);
// I didn't check if list is null. You should do it

你必须更新这个活动列表,并从列表中删除已移除的通知。您可以通过为通知设置setDeleteIntent来使用它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接