简体   繁体   English

推送通知会覆盖以前的通知

[英]Push notification overrides previous notifications

I'm using codeigniter-gcm library on top of codeigniter to send messages to Google Cloud Messaging service. 我在codeigniter顶部使用了codeigniter-gcm库,以将消息发送到Google Cloud Messaging服务。 It sends the message and the message is received at the mobile device, but if I send multiple messages, only the latest message appears on the device (as if it is overriding the previous messages). 它发送消息并在移动设备上接收到该消息,但是如果我发送多条消息,则设备上只会显示最新消息(好像它覆盖了先前的消息一样)。

I'm seeing that I might need to create a unique notification ID, but I'm not seeing how it's done anywhere on the codeigniter-gcm documentation or Google's documentation for downstream messages. 我看到可能需要创建一个唯一的通知ID,但在codeigniter-gcm文档或Google的下游消息文档中的任何地方都看不到它是如何完成的。

Any idea how this should be done? 任何想法应该怎么做?

Here's my code in the codeigniter controller. 这是我在codeigniter控制器中的代码。 It is worth mentioning that Google's response contains a different message_id for each time I send a push... 值得一提的是,每次我发送推送时,Google的响应都包含一个不同的message_id ...

public function index() {
    $this->load->library("gcm");
    $this->gcm->setMessage("Test message sent on " . date("d.m.Y H:i:s"));
    $this->gcm->addRecepient("*****************");
    $this->gcm->setData(array(
        'title' => 'my title',
        'some_key' => 'some_val'
    ));
    $this->gcm->setTtl(false);
    $this->gcm->setGroup(false);
    if ($this->gcm->send())
        echo 'Success for all messages';
    else
        echo 'Some messages have errors';

    print_r($this->gcm->status);
    print_r($this->gcm->messagesStatuses);
}

After three exhausting days I found the solution. 经过三天的疲倦,我找到了解决方案。 I'm posting it here in hope of saving someone else's time... 我将其发布在这里,希望节省其他人的时间...

I had to add a parameter to the data object inside the greater JSON object, named "notId" with a unique integer value (which I chose to use a random integer from a wide range). 我必须在更大的JSON对象内的数据对象中添加一个名为“ notId”的参数,该参数具有唯一的整数值(我选择使用很大范围内的随机整数)。 Now why Google didn't include this in their docs? 现在,为什么Google不将其包含在他们的文档中? Beats me... 甘拜下风...

Here's how my JSON looks now, when it creates separate notifications instead of overriding: 这是我的JSON现在的外观,当它创建单独的通知而不是覆盖时:

{
    "data": {
                "some_key":"some_val",
                "title":"test title",
                "message":"Test message from 30.09.2015 12:57:44",
                "notId":14243
            },
    "registration_ids":["*******"]
}
  • Edit: I'm now thinking that the notId parameter is not really determined by Google, but by a plugin I use on the mobile app side. 编辑:我现在认为notId参数不是由Google真正确定的,而是由我在移动应用程序端使用的插件确定的。 To extend further on my environment, my mobile app is developed using Phonegap, so to get push notification I use phonegap-plugin-push which I now see in its docs that parameter name. 为了进一步扩展我的环境,我的移动应用程序是使用Phonegap开发的,因此要获取推送通知,请使用phonegap-plugin-push ,现在在其文档中看到该参数名称。 I'm kinda' lost now as far as explaining the situation - but happy it is no longer a problem for me :-) 就解释情况而言,我有点迷失了-但很高兴,这对我而言不再是问题:-)

You need to pass a unique ID to each notification. 您需要为每个通知传递唯一的ID。 Once you have clicked on the notification you use that ID to remove it. 单击通知后,便可以使用该ID将其删除。

    ...
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancel(SIMPLE_NOTFICATION_ID_A);
...

But I'm sure you shouldn't have so much of notifications for user at once. 但是我敢肯定,您一次不会收到太多通知给用户。 You should show a single notification that consolidates info about group of events like for example Gmail client does. 您应该显示一条通知,该通知用于合并有关事件组的信息,例如Gmail客户端。 Use Notification.Builder for that purpose. 为此使用Notification.Builder

NotificationCompat.Builder b = new NotificationCompat.Builder(c);
   b.setNumber(g_push.Counter)
    .setLargeIcon(BitmapFactory.decodeResource(c.getResources(), R.drawable.list_avatar))
    .setSmallIcon(R.drawable.ic_stat_example)
    .setAutoCancel(true)
    .setContentTitle(pushCount > 1 ? c.getString(R.string.stat_messages_title) + pushCount : title)
    .setContentText(pushCount > 1 ? push.ProfileID : mess)
    .setWhen(g_push.Timestamp)
    .setContentIntent(PendingIntent.getActivity(c, 0, it, PendingIntent.FLAG_UPDATE_CURRENT))
    .setDeleteIntent(PendingIntent.getBroadcast(c, 0, new Intent(ACTION_CLEAR_NOTIFICATION), PendingIntent.FLAG_CANCEL_CURRENT))
    .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
    .setSound(Uri.parse(prefs.getString(
            SharedPreferencesID.PREFERENCE_ID_PUSH_SOUND_URI,
            "android.resource://ru.mail.mailapp/raw/new_message_bells")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM