6

我正在開發需要顯示通知的應用程序。 對於通知,我使用FireBase Cloud Messaging (FCM)。 我可以在應用程序處於後臺時獲取通知。在FCM中點擊通知時打開特定活動

但是,當我點擊通知,它重定向到home.java頁面。我希望它重定向到Notification.java頁面。

因此,請告訴我如何在通知單擊時指定活動。 我使用兩種服務:

1)MyFirebaseMessagingService

2)MyFirebaseInstanceIDService

這是我onMessageReceived(代碼示例)在MyFirebaseMessagingService類方法。

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "FirebaseMessageService"; 
Bitmap bitmap; 


public void onMessageReceived(RemoteMessage remoteMessage) { 



    Log.d(TAG, "From: " + remoteMessage.getFrom()); 

    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
    } 

    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) { 
     Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
    } 

    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 
} 
/** 
* Create and show a simple notification containing the received FCM message. 
*/ 

private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) { 
    Intent intent = new Intent(this, Notification.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    intent.putExtra("Notification", TrueOrFalse); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setLargeIcon(image)/*Notification icon image*/ 
      .setContentTitle(messageBody) 
      .setStyle(new NotificationCompat.BigPictureStyle() 
      .bigPicture(image))/*Notification with Image*/ 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 

/* 
*To get a Bitmap image from the URL received 
* */ 
public Bitmap getBitmapfromUrl(String imageUrl) { 
    try { 
     URL url = new URL(imageUrl); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap bitmap = BitmapFactory.decodeStream(input); 
     return bitmap; 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return null; 

    } 
} 
+0

刪除 「通知」,也將努力 –

+0

感謝:

public void onMessageReceived(RemoteMessage remoteMessage){ String title=remoteMessage.getNotification().getTitle(); String message=remoteMessage.getNotification().getBody(); String click_action=remoteMessage.getNotification().getClickAction(); Intent intent=new Intent(click_action); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT); NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this); notificationBuilder.setContentTitle(title); notificationBuilder.setContentText(message); notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); notificationBuilder.setAutoCancel(true); notificationBuilder.setContentIntent(pendingIntent); NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0,notificationBuilder.build()); } 

您的通知雲功能/服務器代碼期待你的答覆! –

+0

你的意思是說,從getNotification()中移除getNotification()。getBody(); ?? –

回答

0

當應用程序在後臺意圖必須在發射活動。所以它會打開你的啓動activity.Now你檢查是否有在意向書中發射活動然後啓動所需的活動數據傳遞。

10

根據FCM文件Receive and handle notifications

  • 通知時,您的應用程序在後臺交付。在這種情況下,通知將傳遞到設備的系統托盤。 默認情況下,用戶點擊通知將打開應用程序啓動器。
  • 帶有通知和數據有效負載的消息,包括背景和前景。在這種情況下,通知將傳遞到 設備的系統托盤,並且數據有效負載將通過您的啓動器活動意圖的附加組件 來傳遞。

手柄通知消息在背景執行的應用程序:

當你的應用程序是在後臺,Android的指示通知消息到系統托盤。用戶點擊通知會默認打開應用程序啓動器。

這包括包含通知和數據負載(以及通知控制檯發送的所有消息)的消息。在這些情況下,通知將傳遞到設備的系統托盤,並且數據有效載荷將按照啓動程序活動的目的附加內容進行傳遞。

隨着FCM,您可以發送兩種類型的消息給客戶:

1.通知消息:有時認爲是「顯示的消息。」

FCM代表客戶端應用程序自動向最終用戶設備顯示消息。通知消息具有一組預定義的用戶可見密鑰。

2.數據信息:由客戶端應用程序處理。

客戶端應用程序負責處理數據消息。數據消息只有自定義的鍵值對。

結帳這裏現在越來越About FCM Messages

設置click_action通知有效載荷:

所以,如果你要處理的消息在後臺來了,你必須發送click_action與消息。 click_actionparameter of the notification payload

如果你要打開你的應用程序,並執行特定操作,在通知有效載荷設置click_action並將其映射到您要啓動活動的意圖過濾器。例如,設置到click_actionOPEN_ACTIVITY_1觸發等的意圖過濾器執行以下操作:

<intent-filter> 
    <action android:name="OPEN_ACTIVITY_1" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

FCM有效載荷看起來像下面:

{ 
    "to":"some_device_token", 
    "content_available": true, 
    "notification": { 
     "title": "hello", 
     "body": "test message", 
     "click_action": "OPEN_ACTIVITY_1" 
    }, 
    "data": { 
     "extra":"juice" 
    } 
} 
+3

很好地複製粘貼,但這是什麼答案? –

+0

@TimCastelijns檢查我的更新答案。 –

0

的AndroidManifest.xml

<activity android:name="YOUR_ACTIVITY"> 
    <intent-filter> 
     <action android:name="com.example.yourapplication_YOUR_NOTIFICATION_NAME" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

您[R FirebaseMessagingService.java文件onMessageReceived方法:從您的FCM有效載荷

notification: { 
     title: "TITLE OF NOTIFICATION", 
     body: "NOTIFICATION MESSAGE", 
     sound: "default", 
     click_action: "com.example.myapplication_YOUR_NOTIFICATION_NAME" 
    }