1

我正在使用GCMBaseIntentService。 在我的serive类中扩展了GCMBaseIntentService,我重写了onMessage(Context,Intent)方法。 代码如下: protected void onMessage(Context context,Intent intent)Log.i(TAG,“Received message”);从通知中打开活动-Android

//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。它仅在第一次显示正确的值,然后对于每个通知它始终显示第一个值。请帮忙!

回答

0

试试这个

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

请注意待定意图getActivity的requestCode应unique.So只是通过

为每notification.You

独特的价值可以把时间戳甚至ID您从服务器接收

。我曾经尝试这样做,这works.Do分享您的反馈

+0

好thnx ..我会尝试! – gaurav414u

+0

它不工作,仍然发生同样的事情! – gaurav414u

+0

您能够获取多个通知,或者您一次只检查一个通知。还有你检查是否String id = intent.getExtras()。getString(“event_id”);你传递给generateNotification的值是不同的? – curious

0
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
+0

它不工作...仍然发生同样的事情.. – gaurav414u

+0

PendingIntent intent = PendingIntent.getActivity(context,0,notificationIntent ,** Intent.FLAG_ACTIVITY_NEW_TASK **); – avesha

0
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); 
    } 

这是我在哪里retreiving此事项标识

0

你以后Intent s为相差不大,不足以我EventDescription活动,因此他们重新被回收。特别是,从系统的角度来看,更改extras并没有形成明确的意图。

Intent.filterEquals的文档解释了您需要更改以下一项或多项:操作,数据,类型,类和类别。因此,一种方法是将一些区分信息(在您的案例中是event_id)粘贴到数据URI中;您不必使用此URI进行任何操作,但它可以确保您的意图不同。

还有更多的信息在这related question