0

好吧,我現在正在做的是通過FCM推送通知,這一切進展順利。現在我可以在應用程序處於前臺時更改活動,但是如何在通知面板中點擊通知時如何更改它?需要幫忙。如何打開推送通知的特定活動?

我的代碼:

public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) { 

     // Check for empty push message 
     if (TextUtils.isEmpty(message)) 
      return; 

     // notification icon 
     final int icon = R.mipmap.ic_launcher; 

     // on click activity for the notification !!!!!!!!!! 
     intent = new Intent(Intent.ACTION_MAIN); 
     intent.setClass(mContext, TestActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
     final PendingIntent resultPendingIntent = 
       PendingIntent.getActivity(
         mContext, 
         0, 
         intent, 
         PendingIntent.FLAG_CANCEL_CURRENT 
       ); 

     final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       mContext); 

回答

1
private void sendNotification(String msg) { 
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    long notificatioId = System.currentTimeMillis(); 

    Intent intent = new Intent(getApplicationContext(), TestActivity.class); // Here pass your activity where you want to redirect. 

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, 0); 

    int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
    if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){ 
     currentapiVersion = R.mipmap.ic_notification_lolipop; 
    } else{ 
     currentapiVersion = R.mipmap.ic_launcher; 
    } 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(currentapiVersion) 
      .setContentTitle(this.getResources().getString(R.string.app_name)) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
      .setContentText(msg) 
      .setAutoCancel(true) 
      .setPriority(Notification.PRIORITY_HIGH) 
      .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND) 
      .setContentIntent(contentIntent); 
    mNotificationManager.notify((int) notificatioId, notificationBuilder.build()); 
} 
+0

謝謝你的回答。幫助解決了很多問題。 –

0
Pass your Activity you want to open when clicked into Intent. 
Intent notificationIntent = new Intent(context, XYZActivity.class); 

complete code.. 
    NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 

     Intent notificationIntent = new Intent(context, XYZActivity.class); 

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

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

     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, notification); 
+0

我也一樣,但仍然保持它的開放主要活動。 –

0
 NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent notificationIntent = new Intent(mContext,ACTIVITY_TO_BE_DISPLAYED.class); // Replace ACTIVITY_TO_BE_DISPLAYED to Activity to which you wanna show 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent intent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext) 
       .setAutoCancel(true) 
       .setTicker("YOUR_TICKER_MSG") 
       .setSmallIcon(R.drawable.ic_notification_icon) 
       .setLargeIcon(icon) 
       .setContentTitle("YOUR_TITLE") 
       .setContentText("YOUR_TEXT") 
       .setContentIntent(intent); 
     notificationManager.notify(10, builder.build()); 
相關問題