push silent notification through GCM to Android/IOS

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

I'm seeking to send an state update to an application, which can (or not), be silent.

For example, if the user gets a new message from one of their friends I want to send such message and be able to display an alert with sound.

But in the silent case, I want the user not to notice such notification. it must be gathered by the application; for example if my user has two devices and he changes his name in one of them, I want to send the other device (which is sleeping) a silent name update, that should trigger a change (be dispatched to the application) in background.

According to what I've read this is actually possible, but I'm quite confused regarding to how it actually is, or how it should be done, and there are some contradictions at times. As a backend developer I can do anything but I need to make sure that it works for the frontend guys.

Here some relevant topics:

Silent background push on iOS and Android

Push Notifications without alert

iPhone push notification without alert

Android Silent Push

回答1:

Android:

As long as you do not include a notification tag in your payload and put a data tag in it you get a silent notification

this example would show a notification

{ "notification": {     "title": "Portugal vs. Denmark",     "text": "5 to 1"   },   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..." } 

this would not show a notification

{    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",    "data" : {      "Nick" : "Mario",      "body" : "great match!",      "Room" : "PortugalVSDenmark"    },  } 

go here to read more https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

iOS

add the tag contentAvailable: 1 to your json payload and you get a silent notification

its that simple



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!