【问题标题】:Showing big view notifications with Firebase Cloud Messaging使用 Firebase 云消息传递显示大视图通知
【发布时间】:2017-09-21 20:55:10
【问题描述】:

Firebase Cloud Messaging documentation 中,没有提到大视图/扩展布局的通知。

当应用在后台时,我应该如何显示大视图通知?根据faq,在FirebaseMessagingServiceonMessageReceived 中创建自定义通知似乎是不可能的:

当您的应用在后台时,通知消息会显示在系统托盘中,并且不会调用 onMessageReceived。对于带有数据负载的通知消息,通知消息显示在系统托盘中,并且可以从用户点击通知时启动的意图中检索通知消息中包含的数据。

【问题讨论】:

  • 当您的应用在后台时,可以触发onMessageReceived()如果您使用data- 消息负载。请参阅 Handling Messages docs 以了解取决于您发送的消息负载的行为。

标签: android firebase notifications android-notifications firebase-cloud-messaging


【解决方案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);
    }

}

【讨论】:

    猜你喜欢
    • 2021-07-22
    • 1970-01-01
    • 2021-12-19
    • 2016-12-25
    • 2017-09-04
    • 1970-01-01
    • 2016-12-13
    • 2017-07-31
    • 1970-01-01
    相关资源
    最近更新 更多