1

Firebase Cloud Messaging documentation中,沒有提及有大視圖/擴展布局的通知。使用Firebase Cloud Messaging顯示大視圖通知

應用程序背景時應該如何顯示大視圖通知?在FirebaseMessagingServiceonMessageReceived創建自定義通知根據本faq似乎不可能:

當你的應用程序是在後臺,通知消息將顯示在系統托盤中,並onMessageReceived不叫。對於具有數據有效載荷的通知消息,通知消息顯示在系統托盤中,並且可以從用戶點擊通知時啓動的意圖中檢索通知消息中包含的數據。

+1

當您的應用程序處於後臺時,可能觸發'onMessageReceived()'***如果您使用'data'- * only *消息負載。請參閱[處理消息文檔](https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages)瞭解取決於您發送的消息負載的行爲。 –

回答

1

使用數據對象發送要查看的通知。您可以基本上將所需的所有內容放在數據對象中,並始終通過onMessageReceived方法接收它。這是一個例子。

public class AppFireBaseMessagingService extends FirebaseMessagingService { 

    private final static int REQUEST_CODE = 1; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Map<String, String> data = remoteMessage.getData(); 
     if (data == null) return; 

     if (data.containsKey("title") && data.containsKey("message")) { 
      showNotification(data.get("title"), data.get("message")); 
     } 
    } 

    private void showNotification(String title, String body) { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
       .setContentTitle(title) 
       .setSmallIcon(R.drawable.notification_icon); 

     if (body != null && !body.isEmpty()) { 
      builder.setStyle(new NotificationCompat.BigTextStyle().bigText(body)); 
      builder.setContentText(body); 
     } 

     Intent intent = new Intent(this, MainActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     builder.setContentIntent(contentIntent); 
     builder.setAutoCancel(true); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification n = builder.build(); 
     n.defaults = Notification.DEFAULT_ALL; 
     notificationManager.notify(0, n); 
    } 

} 
相關問題