0

我有Android應用程序,並試圖收到推送通知。 我的問題,當點擊保留的通知打開主要活動, 我需要收到來自firebase的推送通知直接打開特定的活動。這是我的代碼打開特定的活動時,e從火力點推送通知

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     super.onMessageReceived(remoteMessage); 
     Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message")); 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.app_logo_a) 
       .setContentTitle(remoteMessage.getData().get("title")) 
       .setContentText(remoteMessage.getNotification().getBody()); 
     NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     manager.notify(0, builder.build()); 
     try { 
      JSONObject json = new JSONObject(remoteMessage.getData().toString()); 
      handleDataMessage(json); 
     } catch (Exception e) { 
      Log.e("Mo", "Exception: " + e.getMessage()); 
     } 
     Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class); 
     startActivity(resultIntent); 
    } 

    private void handleDataMessage(JSONObject json) { 

     if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 
      // app is in foreground, broadcast the push message 
      Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); 
      pushNotification.putExtra("message", message); 
      LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 
      NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); 
      notificationUtils.playNotificationSound(); 

      Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class); 
      startActivity(resultIntent); 
     } else { 
      Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class); 
      startActivity(resultIntent); 
     } 
    } 
+0

檢查這one .. https://stackoverflow.com/questions/40718155/open-specific-activity-when-notification-clicked-in-fcm –

+0

您是否嘗試在您收到推送通知時或用戶「點擊「UI中的通知? – Pelocho

回答

0

您可以爲您的通知設置掛起的意圖。所以,首先要創建一個懸而未決的意圖:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Destination.class), 0); 

然後添加到您的NotificationBuilder,其中定義標題,描述等。通知,下列參數:

builder.setContentIntent(contentIntent) 

UPDATE:

你有你的NotificationCompatBuilder已經在你的代碼。

替換此:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.app_logo_a) 
       .setContentTitle(remoteMessage.getData().get("title")) 
       .setContentText(remoteMessage.getNotification().getBody()); 

與此:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Destination.class), PendingIntent.FLAG_UPDATE_CURRENT); 
NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.app_logo_a) 
       .setContentTitle(remoteMessage.getData().get("title")) 
       .setContentText(remoteMessage.getNotification().getBody()) 
       .setContentIntent(contentIntent); 
+0

哪裏?添加這個?! –

+0

@MoayedAlayseh我已經更新了我的答案。 –

+0

無法解析setContentIntent !!! –

0

您應該考慮使用PendingIntent能夠在通知時,點擊啓動一項具體活動。添加到您的代碼

Intent intent = new Intent(this, YourActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, YOUT_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(contentIntent); 

您可以更改您的代碼即使您沒有implmented的handleDataMessage很好,但上的通知,當點擊這將讓你開始活動

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    super.onMessageReceived(remoteMessage); 
    Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message")); 

    //two lines addded 
    Intent intent = new Intent(this, YourActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.app_logo_a) 
      .setContentTitle(remoteMessage.getData().get("title")) 
      .setContentText(remoteMessage.getNotification().getBody()) 
      .setContentIntent(contentIntent);//added line 
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(0, builder.build()); 
    try { 
     JSONObject json = new JSONObject(remoteMessage.getData().toString()); 
     handleDataMessage(json); 
    } catch (Exception e) { 
     Log.e("Mo", "Exception: " + e.getMessage()); 
    } 

} 

private void handleDataMessage(JSONObject json) { 

    if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 
     // app is in foreground, broadcast the push message 
     Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); 
     pushNotification.putExtra("message", message); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 
     NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); 
     notificationUtils.playNotificationSound(); 

    } 
} 
+0

哪裏?添加這個?! –

+0

@MayayedAlayseh檢查我更新的答案 –