2014-09-27 70 views
2

我使用下面的代码创建一个通知:如何在不影响未决意图及其有效载荷的情况下更新通知?

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中的目的地类别保持不变。

更新通知时是否有办法保留有效负载?

+0

当您启动一个服务,您可以考绩你的对象的服务,让您可以在服务访问它。 – 2014-09-27 17:24:55

+0

@MisaghEmamverdi nope,那是不可能的。该服务启动一次,并更新所有通知处于活动状态。 – Ashwin 2014-09-27 17:27:56

+0

你的意思是服务应该将不同的额外对象传递给待处理的意图? – 2014-09-27 17:36:27

回答

4

您正在为所有通知设置相同标识的待处理意图。

使用本的

PendingIntent pIntent = PendingIntent.getActivity(this, notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

代替

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

是的男人。有效。那么完全使用这个第二个参数是什么 - requestCode? Android开发人员文档中没有太多内容 – Ashwin 2014-09-28 12:47:54

0

但是我不知道它是唯一的出路或最好的方式,它可以帮助你:

首先定义你的类应该传递给意图解析器方法。你需要这2种方法:通过调用(例如第一主动通知

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(); 

在你的服务,你会进入到你的对象:

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

那么你一定要展现出新的通知,每次添加此):

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来进行通知。

相关问题