Android - 如何发送GCM推送通知以及要加载哪些活动的说明?

Gen*_*nik 6 android push-notification google-cloud-messaging

我能够创建推送通知.但目前我只能让人们登陆主屏幕.

如何将人员发送到特定活动?是否可以添加一些像item_id这样的参数,以便活动知道要加载哪些数据?

或者,如果某个地方有一个很好的教程,那也会很棒.我似乎无法通过谷歌搜索找到很多关于此的信息.

在我的GCMIntentService中,我有这个方法:

      @Override
      protected void onMessage(Context ctxt, Intent message) 
      {           
        Bundle extras=message.getExtras();

        try
        {
            String question_id = extras.getString("question_id");
//          SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( this );
//          Intent intent = new Intent(ctxt, QuestionActivity.class);

            generateNotification(ctxt, extras.getString("message"), "New Message"  );           
        }
        catch ( Exception e )
        {
        }
      }
Run Code Online (Sandbox Code Playgroud)

但我不知道如何更改generateNotification以指示该人应该登陆的活动.谢谢!

Rya*_*yan 10

更新:给予Eran对JSON的信任,我只想详细说明.

您可以使用数据键添加其他参数:

{
   "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
   "data": {
       "stuff": "100",
       "more": "abc"
   },
}
Run Code Online (Sandbox Code Playgroud)

然后使用相同的方式访问intent.getExtras().getString("stuff").

一切都在这里.

然后在你的generateNotifcation():

private static void generateNotification(Context context, String message) {
    NotificationManager notificationManager = (NotificationManager)
        context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, message, when);
    String title = "...";


    //get id from json here and decide which activity to go to...
    Intent notificationIntent = new Intent(context, someClass.class);


    notificationIntent.putExtra("message",message);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.defaults|=Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);
}
Run Code Online (Sandbox Code Playgroud)