android - 前台 android 通知未显示(奥利奥)

标签 android firebase push-notification firebase-cloud-messaging android-8.0-oreo

当应用位于前台时,我尝试使用 Firebase 显示通知。当我从服务器推送通知时,会调用 onMessageReceived 方法,但通知未显示

这是我的代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(final RemoteMessage remoteMessage) {
        Timber.d("FCM-From: " + remoteMessage.getFrom());

            new Handler(Looper.getMainLooper()).post(new Runnable() {
                public void run() {
                    if (remoteMessage.getNotification() != null) {
                        Timber.d("FCM-Message Notification Body: " + remoteMessage.getNotification().getBody());

                        NotificationCompat.Builder builder = new  NotificationCompat.Builder(
                                getApplicationContext(), "CHANNEL_NOTIF")
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .setContentTitle("test")
                                .setContentText("test content");
                        NotificationManager manager = (NotificationManager)     getSystemService(NOTIFICATION_SERVICE);
                        if (manager != null) {
                            Timber.d("FCM-Notif");
                            manager.notify(1, builder.build());
                        }
                    }
                }
            });
    }
}

在我的 logcat 中我可以看到:

FCM-Message Notification Body: 202018105166

FCM-From: 1049809400953

FCM-Notif

我关注了https://developer.android.com/training/notify-user/build-notification.html#builder

我正在奥利奥上运行

解决方案

在这里找到答案:Android foreground service notification not showing

这是一个奥利奥问题,感谢@Yashaswi N P

最佳答案

发生这种情况是因为 Android Oreo 和更高的 API 级别。因此,您必须在 Android 8.0 及更高版本上发布任何通知之前创建通知 channel ,您应该在应用程序启动后立即执行此代码。重复调用此方法是安全的,因为创建现有通知 channel 不会执行任何操作。

并使用以下代码来解决这个问题:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
    private NotificationUtils notificationUtils;
    private String title,message,click_action;
    private  String CHANNEL_ID = "MyApp";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {

            try {
                JSONObject data = new JSONObject(remoteMessage.getData());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
             title = remoteMessage.getNotification().getTitle(); //get title
             message = remoteMessage.getNotification().getBody(); //get message
             click_action = remoteMessage.getNotification().getClickAction(); //get click_action

            Log.d(TAG, "Notification Title: " + title);
            Log.d(TAG, "Notification Body: " + message);
            Log.d(TAG, "Notification click_action: " + click_action);

            sendNotification(title, message,click_action);
        }
    }

    private void sendNotification(String title,String messageBody, String click_action) {
        Intent intent = new Intent(this, TargetActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

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

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.logo_heart)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)// Set the intent that will fire when the user taps the notification
                .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        // notificationId is a unique int for each notification that you must define
        notificationManager.notify(1, mBuilder.build());

        createNotificationChannel();
    }

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.app_name);
            String description = getString(R.string.description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

}

关于android - 前台 android 通知未显示(奥利奥),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48701311/

相关文章:

java - 设置嵌套的 TextView 文本

javascript - 如何使用 Firebase 合并多个集合?

Firebase:Firebase 中未显示事件属性数据

ios - 推送通知无法通过 TestFlight 在 iOS 10 上运行

iOS 推送通知设置 - 拒绝权限与从未请求权限

java - 如何从后台删除 Activity ?启动 Activity 结果

android - 捕获 Intent 异常和/或返回码?

android - 多个 toMany 关系到单个表

java.lang.IllegalStateException : Default FirebaseApp is not initialized in this process com. example.bottomnav

json - 指导如何实际加密 APNS 的 JSON token