栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

不调用GcmListenerService.onMessageReceived()

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

不调用GcmListenerService.onMessageReceived()

正如这个
Github问题中所解释的,这正是您的问题:

来自https://developers.google.com/cloud-
messaging/server#notifications_and_data_messages
“ GCM将代表客户端应用程序显示通知部分。
提供可选数据后,一旦用户单击通知,它将发送到客户端应用程序并打开客户端应用程序。[…]在Android上,数据有效载荷可以在用于启动你的活动的意图取回。

因此,在 用户点击通知后 ,数据便以用于启动活动的意图传递。这意味着您需要执行以下操作:

  • 将click_action添加到您从服务器发送的通知键中:例如
        send_queue.append({'to': REGISTRATION_ID,        'message_id': random_id(),        "notification" : {"body" : "Hello from Server! What is going on? Seems to work!!!","title" : "Hello from Server!","icon" : "@drawable/ic_school_white_48dp","sound": "default","color": "#03A9F4","click_action": "OPEN_MAIN_ACTIVITY"         },        'data': { 'message': "Hello" }})

有关通知有效负载的信息,请参见以下网址:https :
//developers.google.com/cloud-messaging/server-ref#notification-payload-
support

  • AndroidManifest.xml
    用户单击通知后要在要打开的活动上添加意图过滤器时,使用与您在服务器端的“ click_action”键上使用的动作名称相同的动作名称,例如:
        <activity        android:name=".ui.MainActivity"        android:label="@string/title_activity_main" >        <intent-filter> <action android:name="OPEN_MAIN_ACTIVITY" /> <category android:name="android.intent.category.DEFAULT" />        </intent-filter>    </activity>
  • 得到的意图的数据在你 的onCreate() 方法 onNewIntent() ,如果你已经设置的launchMode到singleTop的活动要启动单击该通知时,如:
        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Intent intent = getIntent();        if (intent.hasExtra(Constants.KEY_MESSAGE_TXT)) { String message = intent.getStringExtra(Constants.KEY_MESSAGE_TXT); Log.d(TAG, message);        }     }

我已经对此进行了测试,可以确认它是否有效。(使用XMPP连接)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/415741.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号