重置收件箱样式通知的邮件编号

时间:2016-06-20 18:52:50

标签: android push-notification android-notifications

编辑:它不允许我接受我自己的答案2天,但我明白了。如果您遇到类似问题,请参阅下面的答案

我正在处理从我的应用程序堆叠推送通知并使用带有摘要文本的InboxStyle显示它们。目前,我最多显示3个通知,如果需要,还会显示“+ x more”消息。为此,我正在跟踪当时的消息数量;但是,我不确定如何在用户点击通知时重置邮件数量。例如,如果用户收到3个通知,然后点击推送通知进入应用程序,我希望下一个通知显示为单个通知,但它当前正在显示“消息消息b消息c + 1更多“。当用户取消通知时,是否有某种类型的侦听器可用于将消息号重置为0?以下代码供参考:

 public class GCMPushReceiverService extends GcmListenerService {

private static final String TAG = "GCMPushReceiverService";
private int objectTypeCode; //DLS
private String contentText;
private String objectKeyGUID; //DLG
private String alert;
private String sound;
private boolean vibrate;
private String contentTitle;
private String collapse_key;
private String tickerText;
private static int message_number = 0;
private static ArrayList<String> messages = new ArrayList<String>();

private static final String MY_GROUP  = "my_group";

//with every new message
@Override
public void onMessageReceived(String from, Bundle data){
    this.objectTypeCode =  Integer.parseInt(data.getString("objectTypeCode"));
    this.contentText = data.getString("contentText");
    this.objectKeyGUID = data.getString("objectKeyGUID");
    this.alert = data.getString("alert");
    this.sound = data.getString("sound");
    this.vibrate = Boolean.parseBoolean(data.getString("vibrate"));
    this.contentTitle = data.getString("contentTitle");
    this.collapse_key = data.getString("collapse_key");
    this.tickerText = data.getString("ticker_Text");

    messages.add(message_number, contentTitle +": " + contentText);
    message_number++;

    Log.d(TAG, "From: " + from);
    sendNotification();
}

private void sendNotification() {
    Intent intent;


   if (objectKeyGUID != null && !objectKeyGUID.equals("")) intent = deepLink();
   else  intent = new Intent(this, LogInPage.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    int requestCode = 0;
    PendingIntent pendingIntent =
            PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);




    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this);
    noBuilder.setSmallIcon(R.drawable.icon);
    noBuilder.setContentText(contentText);
    noBuilder.setTicker(tickerText);
    noBuilder.setContentIntent(pendingIntent);
    noBuilder.setSound(sound);
    noBuilder.setGroup(MY_GROUP);
    noBuilder.setAutoCancel(true);

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
            R.drawable.icon);
    noBuilder.setLargeIcon(largeIcon);



    //if we have more than one message, we want to display them in one notification, inbox style
    //if it's more than 3, display the first three and have a "+ x more" message
    //TODO: reset the message_number when we click the notification 
    if (message_number > 1) {
        noBuilder.setContentTitle(message_number + " messages");

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        for (int i = 0; i < message_number && i < 3; i++)
            inboxStyle.addLine(messages.get(i));
        if (message_number > 3)
            inboxStyle.setSummaryText("+ " + (message_number - 3) + " more");
        inboxStyle.setBigContentTitle(message_number + " Notifications");
        //noBuilder.setContentTitle(message_number + " Notifications");
        noBuilder.setStyle(inboxStyle);
    }

    else {
        noBuilder.setContentTitle(contentTitle);
    }

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, noBuilder.build()); //0 = ID of notification
}

1 个答案:

答案 0 :(得分:0)

我最终通过使用Singleton类解决了这个问题 - 我在单例类中存储了消息的ArrayList以及message_number值。我在onMessageRecieved中调用了getMessageNumber和getMessages,然后在sendNotification方法的末尾再次设置了新值。然后我在我的主活动的onResume侦听器中将message_number的值重置为0,并将ArrayList的值重置为null,这样每当应用程序打开时,通知都会刷新。我发现How to keep track of notifications to know when to show a summary notification很有帮助 - 希望这篇帖子和那篇文章可以帮助其他有类似问题的人