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); 
    } 

} 
相关问题