一些Oreo设备无法接收推送通知

15

三星 S8/S5/J2/Note3 成功地接收到了 Firebase推送通知,无论应用程序是在被杀死、后台还是前台运行。

但是,如果应用程序被杀死,Infinix Note 5(Android One-Oreo)Oppo F9(Oreo)将无法收到推送通知,它们仅在后台或前台运行时正常工作。

我阅读了很多文章,这篇这篇提到中国手机存在此问题,并且有一些用户端的解决方法可以让这些通知在他们的手机上工作。

但我想知道是否有可能从开发方面让通知在每个Android设备上都工作。

我针对API 27进行目标设置,这是我的代码:

public class FirebaseMessagingService  extends com.google.firebase.messaging.FirebaseMessagingService {


  @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        String from = remoteMessage.getFrom();
        Map data = remoteMessage.getData();

        JSONObject jsonObject = new JSONObject();
        Set<String> keys = remoteMessage.getData().keySet();
        for (String key : keys) {
            try {
                jsonObject.put(key, remoteMessage.getData().get(key));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        message= jsonObject.getString("message");

         sendNotification(message, urlToOpen);



    private void sendNotification(final String msg, final String urlToOpen) {


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

        String channelId = getString(R.string.notification_channel_general);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setContentTitle("App Name")
                        .setContentText(msg)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setPriority(Notification.PRIORITY_MAX)
                        .setContentIntent(pendingIntent);

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


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "App Channel",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

    notificationManager.notify(0, notificationBuilder.build());

Gradle

:Gradle 是一种现代化的构建工具,用于自动化构建、测试和发布软件。它支持多种编程语言,并使用 Groovy 或 Kotlin 作为其 DSL(领域特定语言)。
compileSdkVersion 27
defaultConfig {
    applicationId "com.myapp.app"
    minSdkVersion 19
    targetSdkVersion 27
    versionCode 2
    versionName "1"
    multiDexEnabled true
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        // Disable fabric build ID generation for debug builds
        ext.enableCrashlytics = false
    }
}

implementation 'com.google.firebase:firebase-messaging:15.0.0'
implementation 'com.google.android.gms:play-services-location:15.0.0'
implementation 'com.google.android.gms:play-services-auth:15.0.0'
implementation 'com.google.android.gms:play-services-basement:16.0.1'
implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
implementation 'com.google.android.gms:play-services-stats:16.0.1'
implementation 'com.google.android.gms:play-services-tasks:16.0.1'
implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
implementation 'com.google.android.gms:play-services-maps:16.0.0'
6个回答

13

来源 : 在中国ROM上启用后台服务和作业(或FCM)

Infinix Note 5(Android One-Oreo)和Oppo F9(Oreo)如果应用程序被杀死,则无法获取推送通知,如果应用程序在后台或前台运行,则可以正常工作。

在使用(Oxygen OS、MIUI等)的中国ROM中,当从最近使用的应用程序托盘中滑动应用程序时,您的应用程序将被终止(类似于强制停止)。由于这个原因,后台运行的每个任务如服务、作业都会随着应用程序而关闭。即使是高优先级的FCM在中国ROM中也无法实现。

实时问题:

1) 您无法获取用户的位置。因此,如果应用程序依赖实时位置,则不会正常工作。

2) 您将无法接收到高优先级的FCM通知

3) 如果应用程序不在托盘中,则不会执行任何时间特定的任务/作业等等。

解决方案

用户需要在应用程序设置中启用自动启动才能使后台服务/作业保持运行。默认情况下是关闭的。在某些设备上,我们可以使用以下示例代码:

样例代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent();

        String manufacturer = android.os.Build.MANUFACTURER;

        switch (manufacturer) {

            case "xiaomi":
                intent.setComponent(new ComponentName("com.miui.securitycenter",
                        "com.miui.permcenter.autostart.AutoStartManagementActivity"));
                break;
            case "oppo":
                intent.setComponent(new ComponentName("com.coloros.safecenter",
                        "com.coloros.safecenter.permission.startup.StartupAppListActivity"));

                break;
            case "vivo":
                intent.setComponent(new ComponentName("com.vivo.permissionmanager",
                        "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
                break;
        }

      List<ResolveInfo> arrayList =  getPackageManager().queryIntentActivities(intent,
              PackageManager.MATCH_DEFAULT_ONLY);

        if (arrayList.size() > 0) {
            startActivity(intent);
        }
    }

}

谢谢@Nilesh,这个组件能在Infinx设备上启动吗?我正在测试的Infinix设备上,安装了Android One(Oreo 8.1)操作系统。 - Taha Kirmani
@TahaKirmani 很抱歉我不知道 Infinx 设备,但是你是否已经在你的 FirebaseMessagingService 中添加了 android:stopWithTask="false",就像这个答案中所说的 https://dev59.com/71UK5IYBdhLWcg3w4jQv#51129304 - AskNilesh
我在 Android Manifest 中添加了 oppo.permission.OPPO_COMPONENT_SAFE,但仍然出现错误“Caused by: java.lang.SecurityException: Permission Denial: starting Intent { cmp=com.coloros.safecenter/.startupapp.StartupAppListActivity } from ProcessRecord{54211b 26708:com.platute.app/u0a142} (pid=26708, uid=10142) requires oppo.permission.OPPO_COMPONENT_SAFE”。 - Sethuraman Srinivasan
但是这在所有vivo系列和oppo系列中都无法工作,因为它在尝试打开设置屏幕时会显示“未找到活动”。 - khushbu

6
在Oreo版本中,他们引入了推送通知的一种新概念——通道化。您可以在此处了解更多有关通道化的信息。针对Oreo及其以上版本和以下版本,分别实现通知。您可以参考以下答案解决问题
我已经在xamarin.android中解决了这个问题,希望这对您有用。

1
@K K:谢谢,但我已经在推送通知中使用了通道。 - Taha Kirmani
还是不行吗?你收到通知了吗?你检查过了吗? - K K
@K K,我在三星设备上收到通知,但在中国设备上却没有。如果应用程序在后台运行或打开,则中国设备会收到通知。 - Taha Kirmani
1
这是自定义操作系统的问题,事实上,我在OnePlus上遇到了这个问题。你有在OnePlus设备上尝试过吗? - K K

5

这是所有中国设备的问题。一旦您杀掉应用程序...它就会从内存中删除,并不全部活动...但在其他设备制造商的情况下,情况并非如此...他们不会完全从内存中删除应用程序。我也在小米设备上遇到了这个问题......他们有一个锁定应用程序的选项,即使被杀死以清除RAM内存,也会使应用程序保留在内存中。


4
自从Oreo版本开始,有了新的“通知渠道”实现(在Android 8.0(API级别26)中开始),因此您需要使用以下代码创建渠道。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "News Channel";
            String description = "Default Notification News Channel";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = getAppContext().getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }

并覆盖通知构建器如下
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getAppContext(),CHANNEL_ID);

假设您有一个名为 CHANNEL_ID 的值,它是您想要的某个东西。
String CHANNEL_ID = "DEFAULT_NEWS_CHANNEL";

这已经在OnePlus3T设备上进行了测试,因此它应该对您有效。


3
我希望以下代码对您有用。在我的所有设备上它都可以工作。
        Intent notificationIntent = new Intent(context, SplashScreenActivity.class);

        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

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

        // 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 nameChannel = context.getString(R.string.app_name);
            String descChannel = context.getString(R.string.app_name);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(context.getString(R.string.app_name), nameChannel, importance);
            channel.setDescription(descChannel);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            assert notificationManager != null;
            notificationManager.createNotificationChannel(channel);
        }

        PendingIntent pendingIntent = PendingIntent.getActivity((context), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // Create Notification
        NotificationCompat.Builder notification = new NotificationCompat.Builder(context, context.getString(R.string.app_name))
                .setChannelId(context.getString(R.string.app_name))
                .setContentTitle(TextUtils.isEmpty(title) ? getString(R.string.app_name) : title)
                .setContentText(description)
                .setTicker(context.getString(R.string.app_name))
                .setSmallIcon(R.drawable.ic_stat_name)
                .setSound(notificationSound)
                .setLights(Color.RED, 3000, 3000)
                .setVibrate(new long[]{500, 500})
                .setWhen(System.currentTimeMillis())
                .setDefaults(Notification.DEFAULT_SOUND)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        if (result != null) {
            notification.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(result));
        }

        assert notificationManager != null;
        notificationManager.notify(100, notification.build());

我刚刚创建了通知通道,请检查Oreo的条件。

如果你遇到任何问题,请告诉我,我会帮助你解决。

谢谢。


1

我曾经遇到同样的问题,在搜索和尝试之后,我发现用户需要手动启用自启动和电池优化权限。详细信息请参考this链接。


如果这个答案解决了你的问题,请接受/点赞该答案。 - Amin Pinjari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接