简体   繁体   English

Android 7.1.1推送通知和FirebaseMessagingService不起作用

[英]Android 7.1.1 Push Notification and FirebaseMessagingService not work

I am developing an app that works with FirebaseMessagingService and NotificationCompat. 我正在开发与FirebaseMessagingService和NotificationCompat一起使用的应用程序。 My target API is 26. The problem I have is that notifications in android 7.1.1 appear and close at once, they are not kept in the notification bar. 我的目标API是26。我遇到的问题是android 7.1.1中的通知会立即显示并关闭,它们不会保存在通知栏中。 However, in Android 7.0 it works perfectly. 但是,在Android 7.0中,它可以完美运行。 I saw many tutorials and in all they do the same thing that I do. 我看到了许多教程,并且所有这些教程都执行与我相同的操作。 I would like to know what I may be missing. 我想知道我可能会缺少什么。

My Services 我的服务

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public void onMessageReceived(RemoteMessage remoteMessage) {

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

    Map data = remoteMessage.getData();

    if (SinchHelpers.isSinchPushPayload(data)) {
        //comentario                
    } else {            
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload => " + remoteMessage.getData());
            Map<String, String> params = remoteMessage.getData();
            JSONObject object = new JSONObject(params);
            try {
                sendNotification(object.get("message").toString(), object.get("chatId").toString(), object.get("user").toString());
                if (/* Check if data needs to be processed by long running job */ true) {
                    // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
                    scheduleJob();
                } else {
                    // Handle message within 10 seconds
                    handleNow();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.d(TAG, "JSONException => " + e.getMessage());
            }

        }

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

    }
}

} }

private void sendNotification(String messageBody, String chatId, String userName) {
    try {
        Intent intent = new Intent(this, ChatActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXTRA_CHAT_ID", chatId);
        intent.putExtra("EXTRA_USER", userName);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_ONE_SHOT);

        String channelId = "fcm_default_channel";
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher_foreground)
                        .setContentTitle(userName)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setGroup(userName)
                        .setSound(defaultSoundUri)
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .setContentIntent(pendingIntent);

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

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    } catch (Exception e) {
        Log.e(TAG, "userName => " + userName + " => " + e.getMessage());
    }
}

Notification channels: Android 8.0 introduces notification channels that allow you to create a user-customizable channel for each type of notification you want to display. 通知渠道:Android 8.0引入了通知渠道,可让您为要显示的每种通知类型创建用户可定制的渠道。

Please have a look at the documentation. 请查看文档。

Build a notification. 建立通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两种类型的 FCM 推送通知有效负载(通知与数据)之间的区别以及它们何时到达 Android FirebaseMessagingService? - Difference between the two types of FCM push notification payloads (notification vs data) and when do they arrive to Android FirebaseMessagingService? FirebaseMessagingService 推送通知创建 - 如何设置图标? - FirebaseMessagingService push notification creation - how to set icon? 解析推送通知不适用于Android - Parse Push Notification not work on android 无法从 Android 中 FirebaseMessagingService 的 onMessageReceived 生成通知 - Unable to generate notification from onMessageReceived of FirebaseMessagingService in Android 带有 FCM 的 Android 通知 - 谁启动了 FirebaseMessagingService? - Android Notification with FCM - Who started the FirebaseMessagingService? Android-推送通知:FirebaseMessagingService类的用途? - Android - push notifications: purpose of FirebaseMessagingService class? 具有解析的android push通知不起作用 - android push notification with parse doesen't work 推送通知技术如何在 Android 上运行? - How does push notification technology work on Android? 用于离子的pushwoosh推送通知不适用于android - pushwoosh push notification for ionic does not work with android 如何使用GCM在Android上进行推送通知? - How to make push notification with GCM work on android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM