繁体   English   中英

从通知打开活动-Android

[英]Opening an Activity from a notification -Android

我在扩展GCMBaseIntentService服务类中使用GCMBaseIntentService ,我重写了onMessage(Context,Intent) 代码在这里:

protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");

    //String message = getString(R.string.gcm_message);
    String id=intent.getExtras().getString("event_id");
    String message=intent.getExtras().getString("title");
    String eventname=intent.getExtras().getString("event_name");

    generateNotification(getApplicationContext(), id, message, eventname);  
}

private static void generateNotification(Context context, String id, String message, String eventname) 
{
    int icon = R.drawable.ic_stat_gcm;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

   //new intent
    Intent notificationIntent = new Intent(context, EventDescription.class);
    notificationIntent.putExtra("event_id", id);//need this id in the eventdescription activity

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, eventname, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, notification); 
}

问题是,当我单击此通知时,在Eventdescription活动中,从意图中提取额外内容并打印ID。 它仅在第一次时显示正确的值,此后每次通知都始终显示第一个值。 请帮忙!

尝试这个

PendingIntent intent = PendingIntent.getActivity(context, **uniqueKey**, notificationIntent, 0);

请注意,待定意图的getActivity的requestCode应该是唯一的。

每个通知的唯一值。您可以放置​​时间戳甚至收到的ID

从服务器。 我已经尝试过此方法,并且有效。请分享您的反馈意见

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notificationsfinal);


    start=1;
    end=15;

    next=(Button)findViewById(R.id.nextbtn);
    prev=(Button)findViewById(R.id.prevbtn);
    statusview=(TextView)findViewById(R.id.status);


    notification_list_view=(ListView)findViewById(R.id.not_listview);
    updatestatus();
    getNotifications();

}
private void getNotifications()
{
    eventnotifierapp app=(eventnotifierapp)getApplication();



    id=getIntent().getExtras().getString("event_id");
    db=new MyDB(this);
    }

这是我的EventDescription活动,我在其中获取该event_id

您后续的Intent差别不大,因此将其回收。 具体而言,从系统的角度来看,更改extras并没有明显的意图。

Intent.filterEquals的文档说明您将需要更改以下一项或多项:操作,数据,类型,类和类别。 因此,一种方法是将一些区别信息(在您的情况下为event_id )粘贴到数据URI中。 您无需对任何内容使用此URI,但可以确保您的意图是不同的。

有关问题的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM