当前位置: 首页 > 知识库问答 >
问题:

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

濮阳振
2023-03-14

编辑:它不会允许我接受我自己的答案2天,但我想通了。如果你有类似的问题,请看我下面的回答

我正在努力堆叠应用程序中的推送通知,并使用InboxStyle将其与摘要文本一起显示。目前,我最多显示3个通知,如果需要,后面会显示一个“+x more”消息。要做到这一点,我要跟踪消息的数量;但是,我不确定当用户点击通知时如何重置消息数量。例如,如果用户收到3个通知,然后点击推送通知进入应用程序,我希望下一个通知显示为单个通知,但它当前显示的是“消息a消息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个答案

任繁
2023-03-14

最后,我使用了一个Singleton类来解决这个问题--我在Singleton类中存储了消息的ArrayList以及message_number值。我在onMessageRecieved中调用了getMessageNumber和getMessages,然后在sendNotification方法的末尾再次设置新值。然后,在主活动的onResume侦听器中将message_number的值分别重置为0和ArrayList的值为null,这样,每当打开应用程序时,通知就会被刷新。我发现了如何跟踪通知以知道何时显示摘要通知是有帮助的--希望这篇文章和那篇文章能帮助其他遇到类似问题的人

 类似资料:
  • 我想知道gmail应用程序是如何将jellybean收件箱样式通知中每一行的标题设置得不同的,例如“(Google+)、Google Play、Stack Exchange”等都有比它们各自细节更亮的颜色文字。有线索吗? http://developer.android.com/guide/topics/ui/notifiers/notifications.html#bignotify

  • 我尝试过在laravel中使用sendmail,但它不起作用。查看我的帖子通过Laravel发送邮件不一致 所以,我试着在laravel中使用mailtrap的假的smtp服务器。这里也不行。我在Bitnami Mamp堆栈7.1上。15-0,Laravel 5.8,并在当地进行测试。 我按照这篇文章来设置代码 https://blog.mailtrap.io/send-email-in-lara

  • 问题内容: 在使用控制台输出和测试结果作为电子邮件正文的从属作业完成后,我正在使用可编辑电子邮件通知来发送电子邮件。 我还需要在电子邮件中直接添加指向实际构建构建文件夹的链接,是否有一种简单的方法?也许某种令牌?我没有找到有关如何直接链接到构建文件夹以在电子邮件中显示其位置的任何线索。 问题答案: 首先,点击 Content Token Reference* 旁边的问号,这将告诉您所有需要的内容。

  • 在使用IMAP和PHP之前,我从未尝试过获取和移动电子邮件,因此将发送到垃圾箱的电子邮件移回收件箱。 好的,所以我可以正确地从Gmail中提取电子邮件,我可以删除它们,或者将它们移到垃圾箱。 当我尝试将它们移回收件箱时,我收到以下错误: 注意:未知:[TRYCREATE]第0行未知中没有文件夹[Gmail]/收件箱(失败)(errflg=2) 所以很明显,有些事情我不太明白,在过去的几个小时里,我

  • 本文向大家介绍thinkphp实现163、QQ邮箱收发邮件的方法,包括了thinkphp实现163、QQ邮箱收发邮件的方法的使用技巧和注意事项,需要的朋友参考一下 用了很长时间去一步一步摸索,终于先在163 网易邮箱上测试成功了,下面就把这个过程分享给大家。 在进入正题这前先看下网易(163)邮箱的服务器地址和端口号: 一、前期准备 使用网易邮箱,当然要注册个账号,这个就不用我多说了,自己去注册。

  • 我正在通过DocuSign eSignature API成功发送信封,并且对作为签名过程一部分发送的电子邮件有疑问。发送请求时,我会收到以下电子邮件(使用沙盒环境): > 当每个收件人单击链接查看要签署的协议时,向我发送电子邮件(例如,“John Smith查看了Acme共同客户协议”) 协议完成后,向我(和每位收件人)发送电子邮件。 我正试图了解每一项都有哪些选项可供选择,因为: 启用/禁用每封