0

我寫推送通知在Android項目,在MyFirebaseMessagingService服務寫這個代碼顯示了通知:爲什麼點擊通知時不顯示活動?

Intent intent = new Intent(getApplicationContext(), NotificationMessage.class); // 
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 
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) 
    .setContentIntent(contentIntent); 
mNotificationManager.notify((int) notificatioId, notificationBuilder.build()); 

時的主要活動表演,併發送推送,通知是展示和點擊重定向到NotificationMessage活動,但是當我關閉主要活動並再次發送推送時,通知會顯示給我,但是當我點擊它時,重定向到主要活動並且不顯示NotificationMessage活動,會發生什麼?我該如何解決問題?謝謝。

回答

0

試試這個:

private void setNotification(String notificationMessage) { 
    mNotificationManager = getApplication().getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent notificationIntent = new Intent(getApplicationContext(), NotificationMessage.class); 

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

    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

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) 
       .setContentIntent(contentIntent); 
     mNotificationManager.notify((int) notificatioId, notificationBuilder.build()); 

} 
+0

什麼是mNotificationManager? – behzad

+0

requestID是什麼? – behzad

0
public class MainActivity extends AppCompatActivity { 

NotificationManager mNotificationManager; 
Button btn_notifcation; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btn_notifcation= (Button) findViewById(R.id.btn); 
    btn_notifcation.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      addNotification(); 
     } 
    }); 

} 


private void addNotification() { 
    NotificationCompat.Builder builder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle("Notifications Example") 
        .setContentText("This is a test notification"); 


    Intent notificationIntent = new Intent(this, NotificationMessage.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(contentIntent); 

    // Add as notification 
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(0, builder.build()); 
} 

} 
+0

什麼是mNotificationManager? – behzad

+0

通過使用mNotificationManager您可以顯示通知到您的應用程序的標題欄 – swap9

+0

謝謝,但是當我的應用程序在後臺,並且您的代碼運行時,不顯示NotificationMessage活動 – behzad

0
private void generateNotification(Context context, String message) { 
    int icon = R.mipmap.ic_launcher; 
    long when = System.currentTimeMillis(); 
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); 
    String title = context.getString(R.string.app_name); 

    Intent intent = new Intent(context, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 
      PendingIntent.FLAG_ONE_SHOT); 
    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(icon) 
      .setLargeIcon(bitmap) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setNumber(messageCount) 
      .setContentIntent(pendingIntent); 


    notificationManager.notify(0, notificationBuilder.build()); 

} 
+0

你測試了你的代碼嗎?當我的應用程序在後臺,不工作 – behzad

相關問題