2014-03-26 30 views
0

嗯,我正在推进推送通知功能。它在接收到推送消息之后立即开始意图。但是我想在用户点击通知之后开始这个意图。点击推送通知后的开始意图

那么,我该怎么做呢?我怎么能检测到用户点击收到的消息?

下面是我写的代码:

public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    Log.d("ALERT", "Message Received"); 

    Intent screen2 = new Intent(context, Tela2.class); 
    screen2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(screen2); 
} 

它的工作好

回答

0

应该创建并显示通知,并使用挂起的意图指定从通知开始其活动。

例如:

mNotificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      new Intent(context, Tela2.class), 0); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.ic_stat_gcm) 
    .setContentTitle("GCM Notification") 
    .setStyle(new NotificationCompat.BigTextStyle() 
    .bigText(msg)) 
    .setContentText(msg); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

被点击的通知时,该代码就会开始活动。

+0

谢谢,让我试试吧! – Javanes