Android - 我怎样才能发送GCM推送通知,并指示要加载哪些活动? [英] Android - how can I send a GCM push notification with instructions of which activity to load?

查看:99
本文介绍了Android - 我怎样才能发送GCM推送通知,并指示要加载哪些活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



或者如果在某个地方有一个很好的教程,那会是很好。我无法真正看到通过Google搜索找到很多有用的信息。



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

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

尝试
{
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(例外e)
{
}
}

但我不确定如何更改generateNotification以指示此人应该登陆的活动。
Thanks!

解决方案

更新:
让Eran信誉为JSON,我只想详细说明。



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

  { 
registration_ids:[APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx ...],
data:{
stuff:100,
more:abc

}

然后使用 intent.getExtras()。getString(stuff)



全部是 $ b 然后在你的> generateNotifcation()

  private static void generateNotification(Context context,String message){
NotificationManager notificationManager =(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
通知通知=新通知(R.drawable.ic_launcher,message,when);
String title =...;


//从这里获取json的id,并决定要去哪个活动...
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);
}


I am able to create push notifications. But currently I am just able to make people land on the home screen.

How can I send people to a specific Activity? And is it possible to also put add some parameter like item_id so the activity knows what data to load?

Or if there is a good tutorial for this somewhere, that would be great as well. I can't really seem to find much good info on this by googling.

In my GCMIntentService I have this method:

      @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 )
        {
        }
      }

But I am not sure how to change the generateNotification to also signal what Activity the person should land on. Thanks!

解决方案

UPDATE: Give Eran credit for the JSON, I just want to elaborate.

You can add other parameters with the data key:

{
   "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
   "data": {
       "stuff": "100",
       "more": "abc"
   },
}

Then access the same way using intent.getExtras().getString("stuff").

It is all here.

Then in your 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);
}

这篇关于Android - 我怎样才能发送GCM推送通知,并指示要加载哪些活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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