protected void onMessage(Context context,Intent intent)接收null [英] protected void onMessage(Context context, Intent intent) receives null

查看:110
本文介绍了protected void onMessage(Context context,Intent intent)接收null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用过本教程( GCM演示应用 [ ^ ])运行示例gcm-server应用程序。



我已经成功构建了客户端和服务器端代码。



现在,我无法从服务器向客户端应用程序发送自己的消息。



我已经推荐过这个链接(消息 [ ^ ])发送我自己的MessageBuilder



所以,我修改了这样的代码,



服务器端: -

I have used this tutorial (GCM Demo Application[^]) to run the sample gcm-server application.

I have successfully built both client and server side code.

Now, I got stuck on sending my own message to client application from server.

I have referred this link(Message[^]) to send my own MessageBuilder

So, I modified the code like this,

server side:-

Message msg = new Message.Builder().build();





to





to

Message msg = new Message.Builder()
          .collapseKey("collapseKey")
          .timeToLive(30)
          .delayWhileIdle(true)
          .addData("key1", "value1")
          .addData("key2", "value2")
          .build();





客户端: -



client side:-

@Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        String message = intent.getStringExtra("key2");
        //getString(R.string.gcm_message);
        displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
    }





但我仍然在客户端应用程序中''null'' 。请帮帮我..



先谢谢。



But Still I''m getting ''null'' in client application. Please help me..

Thanks in Advance.

推荐答案

确保您发送的按摩是正确的。
Make sure the massages you sent were right.


恕我直言,你试图在新的Message msg = ...声明中做太多事情。将其分解为连续的步骤并查看调试器中每个的结果。



如果你在现实世界中处理我的项目,我不会让你提交看起来像这样的代码。太复杂,太难调试(由您当前的麻烦验证)。将尽可能多的代码打包到一行中不被认为是好的风格。
IMHO you are trying to do too many things in your new "Message msg = ..." statement. Break that up into sequential steps and look at the result of each in the debugger.

If you were working on my project in the real world, I would not let you submit code that looked like that. Too complex, too difficult to debug (as verified by your current trouble). Packing as much code as possible into one line is not considered good style.


这篇关于protected void onMessage(Context context,Intent intent)接收null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆