【问题标题】:react native push notification doesn't work in Android 8.1 (API level 27)反应本机推送通知在 Android 8.1(API 级别 27)中不起作用
【发布时间】:2019-03-18 03:34:57
【问题描述】:

在我的 react native 应用程序中,我尝试使用此库 react-native-push-notification 处理计划的语言环境通知,它在运行 25 岁以下的 android API 的设备上运行良好,因此问题出现在 android Oreo 版本中,我尝试了很多解决方案,例如使用频道并向该频道添加通知,但没有结果,请帮助!!!

我的 react 原生版本:

  • react-native-cli: 2.0.1
  • 反应原生:0.58.5

来自安卓项目:

    buildToolsVersion = "28.0.2"
    minSdkVersion = 16
    compileSdkVersion = 28
    targetSdkVersion = 27
    supportLibVersion = "28.0.0"

【问题讨论】:

    标签: android react-native android-8.0-oreo localnotification react-native-push-notification


    【解决方案1】:

    我花了几天时间寻找解决方案后自己解决了这个问题!

    转到react-native-push-notification库的android项目中的文件并进行一些更改here

    来自line 572,To line

    将这些行替换为:

    NotificationChannel mChannel = manager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
    if (mChannel == null) {
    mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Channel name", NotificationManager.IMPORTANCE_MAX);
    mChannel.enableVibration(true);
    mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    manager.createNotificationChannel(mChannel);
    }
    

    【讨论】:

    • 如果不让我将其添加到您的答案中,我相信您知道所做更改背后的原因。
    【解决方案2】:

    由于电池优化,react 本机推送通知在 >=o 中不起作用...

    在 MAinActivity.java 中添加通道

    public void onResume() {
            super.onResume();
            NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                int importance = NotificationManager.IMPORTANCE_HIGH;
    
                NotificationChannel notificationChannel = new NotificationChannel("channelID", "channelName", importance);
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                notificationChannel.enableVibration(true);
                notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                nMgr.createNotificationChannel(notificationChannel);
            }
    
            nMgr.cancelAll();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      相关资源
      最近更新 更多