活动中的Gcm通知数据

时间:2015-02-06 07:28:20

标签: android google-cloud-messaging android-notifications

我正在使用GCM通知,我已经完成了有关接收数据的所有设置,Gcm通知即将到来但是当我点击通知时,它会打开,但我的意图是空的。

我的聊天屏幕

    protected void onCreate(Bundle savedInstanceState)
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chatscreenctivity);
        Intent intent=getIntent();
        registerReceiver(mHandleMessageReceiver, new IntentFilter(
                DISPLAY_MESSAGE_ACTION));
        String message = intent.getExtras().getString("broadmessage");  **//Hre i got Null**
        String sender_image=intent.getExtras().getString("sender_image");**//Hre i got Null**
        String sender_Name=intent.getExtras().getString("sender_name");
        mlistview=(ListView)findViewById(R.id.listview1);
        sendmessage=(ImageView)findViewById(R.id.sendmeassage);
        sentext=(EditText)findViewById(R.id.et_sent_msg);
        getActionBar().setTitle(sender_Name);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
}





   Hre i got the value from gcm
         @Override
            protected void onMessage(Context context, Intent intent) {
                Log.i(TAG, "Received message");
                String Message ="",image="",sendername="";
                String Broadscast = intent.getExtras().getString("message");
                if(Broadscast.equals("You have got login points!"))
                {
                    Message=Broadscast;
                    generateNotification(context, Message);
                }
                else
                {
                    try
                    {
                    JSONObject json=new JSONObject(Broadscast);
                    Message=json.getString("message");
                    image=json.getString("image");
                    sendername=json.getString("firstname")+" "+json.getString("lastname");

                    }
                    catch(JSONException e)
                    {

                    }
                    generateNotification(context, Broadscast);
                }
                displayMessage(context, Message);
                // notifies user

            }

这是我的自定义通知,我传递了值

 Intent notificationIntent = new Intent(context, SlidingMenuActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.contentIntent=intent;

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification); 


here is diaplaymessage in common utilites
   static void displayMessage(Context context, String message) {
        Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
        intent.putExtra("broadmessage", message);
       intent.putExtra("sender_image", "fdgdfg");
        context.sendBroadcast(intent);
    }

1 个答案:

答案 0 :(得分:0)

在notificationIntent中设置Intent数据,如下所示

 Intent notificationIntent = new Intent(context, SlidingMenuActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra("broadmessage", message);
    notificationIntent.putExtra("sender_image", "fdgdfg");
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.contentIntent=intent;

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);