在android中关闭应用程序时不显示推送通知

在android中关闭应用程序时不显示推送通知,android,firebase,push-notification,android-8.0-oreo,Android,Firebase,Push Notification,Android 8.0 Oreo,我有两个装置。一个运行API 26,另一个运行API 21。我正在使用谷歌firebase进行通知。除非我先打开应用程序,否则API 26设备不会接收推送通知。然后它会收到所有的好消息。我不必先打开应用程序才能在API 27设备上接收推送通知。有没有想过如何在不打开应用程序的情况下始终接收推送?我希望用户只需打开手机即可收到通知 private void sendNotification(String title, String messageBody, String clickAction)

我有两个装置。一个运行API 26,另一个运行API 21。我正在使用谷歌firebase进行通知。除非我先打开应用程序,否则API 26设备不会接收推送通知。然后它会收到所有的好消息。我不必先打开应用程序才能在API 27设备上接收推送通知。有没有想过如何在不打开应用程序的情况下始终接收推送?我希望用户只需打开手机即可收到通知

private void sendNotification(String title, String messageBody, String clickAction) {

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String id = "";
    if (Build.VERSION.SDK_INT >= 26) {
        id = "my_channel_01";
        // The user-visible name of the channel.
        CharSequence name = "myChannel";
        // The user-visible description of the channel.
        String description = "";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(id, name,importance);
        // Configure the notification channel.
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        // Sets the notification light color for notifications posted to this
        // channel, if the device supports this feature.
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        notificationManager.createNotificationChannel(mChannel);
    }


    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, id)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);


    notificationManager.notify(1 /* ID of notification */, notificationBuilder.build());


}

在清单文件中添加BootBroadcastReceiver

<receiver
            android:name=".OnBootBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
</receiver>

在清单文件中添加BootBroadcastReceiver

<receiver
            android:name=".OnBootBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
</receiver>

您是如何从firebase发送通知的?它是来自控制台还是API?我使用API发送post请求。您是如何从firebase发送通知的?它是来自控制台还是API?我使用apiHello Genius通过post请求发送它。非常感谢。不确定为什么FireBase messaging.works到android Lollipop 6.0中的android API 27的任何教程中都不包含这一点。非常感谢。不知道为什么FireBase messaging.works到android Lollipop 6.0中的android API 27的任何教程中都不包含这一点不起作用