2

我需要使用通知点击事件,我有通知方法,但这种方法不打开我的活动。我怎样才能打开活动时通知点击

我的代码:

private void sendNotification(String msg) { 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
     .setContentTitle("EXX") 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setStyle(new NotificationCompat.BigTextStyle() 
     .bigText(msg)) 
     .setContentText(msg)   
     .setOngoing(true);   
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
}  

这是可能的,

感谢。

+3

的可能的复制13716723 /点击通知后打开应用程序) –

+0

不重复。 OP只是缺少'setContentIntent()' –

回答

3

是的,这是可能的。

改变你的方法,就像那样;

private void sendNotification(String msg) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.putExtra("yourpackage.notifyId", NOTIFICATION_ID); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setContentTitle("EXX") 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setStyle(new NotificationCompat.BigTextStyle() 
      .bigText(msg)) 
      .addAction(getNotificationIcon(), "Action Button", pIntent) 
      .setContentIntent(pIntent) 
      .setContentText(msg) 
      .setOngoing(true); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
} 

,并添加您mainactivity

NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

这段代码工作。

+0

哦谢谢!这段代码对我有用!我爱你bro <3 –

+0

不客气。 –

4

嘿@约翰这很简单

你只需要做

Intent intent = new Intent(this, ResultActivity.class); 

... //因为单击通知打开一个新的( “特殊”)的活动,有 //不需要创建一个人工后退栈。

PendingIntent pendingIntent = TaskStackBuilder.create(getApp()) 
      .addNextIntent(intent) 
      .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

而且在mBuilder

private void sendNotification(String msg) { 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
     .setContentTitle("EXX") 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setStyle(new NotificationCompat.BigTextStyle() 
     .bigText(msg)) 
     .setContentText(msg)   
     .setOngoing(true);  
     .setContentIntent(pendingIntent) 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
} 

组待决的意图,你(http://stackoverflow.com/questions/ [点击通知后,打开应用程序]做:)

相关问题