某些奥利奥设备未收到推送通知

时间:2018-10-17 07:29:07

标签: android performance firebase firebase-cloud-messaging android-push-notification

三星S8/S5/J2/Note3成功地杀死Firebase Push Notification的应用是在后台还是在前台

但是如果应用被杀死,Infinix Note 5 (Android One- Oreo)Oppo F9(Oreo)不会收到推送通知,如果应用处于后台或前景,它们可以正常工作。

我浏览了很多文章thisthis,提到中国手机存在此问题,并且从用户方面可以通过一些方法使这些通知在他们的手机上正常工作。

但是我想知道开发方面是否有可能使通知在每个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());

成绩

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 个答案:

答案 0 :(得分:9)

来源Enable background services and Jobs (or FCM) in Chinese ROMs

  

如果应用被杀死,Infinix Note 5(Android One-Oreo)和Oppo F9(Oreo)不会收到推送通知,如果应用处于后台或前景,则它们可以正常工作。

从最近的应用程序托盘中刷过应用程序后,使用(氧气操作系统,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);
        }
    }

}

答案 1 :(得分:6)

在Oreo中,他们引入了一种新的推送通知通道化概念。您可以阅读有关渠道化here的更多信息。分别针对奥利奥矿和风箱矿实施通知。您可以将以下答案引至fix the issue

我已经修复了xamarin.android中的问题,希望这对您有用。

答案 2 :(得分:4)

这是所有中文设备的问题。一旦您杀死了该应用程序...它将被从内存中删除,并且不是所有处于活动状态...但是对于其他设备制造商而言,情况并非如此。不会将应用程序从内存中完全删除。我也遇到了小米设备的问题....他们有一个选项可以锁定应用程序,即使将其杀死以清除内存也可以将其保留在内存中

答案 3 :(得分:4)

自奥利奥以来,就有新的Notification Channels实现(started in Android 8.0 (API level 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设备上进行了测试,因此它应该可以为您工作

答案 4 :(得分: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的条件。

让我知道您是否遇到任何问题。我在这里为您提供帮助。

谢谢。

答案 5 :(得分:1)

我遇到了同样的问题,经过搜索和解决后, 我发现用户需要手动启用自动启动和电池优化权限 有关更多详细信息,请参见this链接