【问题标题】:Notifications in Oreo not working if app is not running如果应用程序未运行,奥利奥中的通知将不起作用
【发布时间】:2018-09-28 15:13:03
【问题描述】:

我正在尝试在我的应用中显示通知。一切似乎都很好,但在 Android 8.0 Oreo 中,如果我终止应用程序,则不会显示通知。在以前的 Android 版本中,这不是问题。

我的源代码是下一个。

public class NotificationHelper extends ContextWrapper {

private static final int NOTIFICATION_ID = 600;
private static final String NOTIFICATION_CHANNEL_ID = "bmedio_v6";         // This is the Notification Channel ID. More about this in the next section
private static final String NOTIFICATION_CHANNEL_NAME = "medio";             //User visible Channel Name
private static final String NOTIFICATION_CHANNEL_DESC = "Medio Channel";             //User visible Channel Name

public NotificationHelper(Context base) {
    super(base);
}

public void showNotification(String title, String message) {

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

        // Configure the notification channel.
        notificationChannel.setDescription(NOTIFICATION_CHANNEL_DESC);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.BLUE);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);

        notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    }else{
        notificationBuilder = new NotificationCompat.Builder(this);
    }

    notificationBuilder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_icon_notify)
            //     .setPriority(Notification.PRIORITY_MAX)
            .setContentTitle(title)
            .setContentText(message);

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

}

}

【问题讨论】:

  • 您使用哪款手机..??

标签: android notifications android-8.0-oreo


【解决方案1】:

使用 'com.google.android.gms:play-services-gcm' 9.6.0 或更高版本的库可以解决问题。我使用的是 GCM,而不是 FCM。

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,然后我找到了解决方案,您需要从电池优化列表中删除您的应用程序标记您的应用程序未优化,我在onePlus5,在杀死一个应用程序后,通知也起作用了。

    步骤

    1. 转到设置
    2. 点击电池选项
    3. 点击电池优化,会显示正在优化的应用列表。
    4. 找到您的应用选择不优化并完成!

    现在我发现了,所以现在我需要看看如何使用代码来做到这一点。 同时让我知道您是否遇到同样的问题,否则它将解决您的问题。 :

    【讨论】:

      【解决方案3】:
              Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      
              PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
      
              NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
      
              inboxStyle.addLine(message);
      
              NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
      
              if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),"channel_01")
                          .setSmallIcon(R.mipmap.ic_launcher)
                          .setContentText(message)
                          .setContentIntent(resultPendingIntent)
                          .setAutoCancel(true)
                          .setPriority(Notification.PRIORITY_HIGH)
                          .setChannelId("channel_01")
                          .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                          .setStyle(inboxStyle)
                          .setContentTitle(Title);
      
                  mNotificationManager.notify(Notification_ID, mBuilder.build());
      
                  NotificationChannel channel = new NotificationChannel(Notification_ID, "Playback Notification", NotificationManager.IMPORTANCE_HIGH);
                  channel.enableLights(true);
                  channel.enableVibration(true);
                  channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                  assert mNotificationManager != null;
                  mBuilder.setChannelId("channel_01");
                  mNotificationManager.createNotificationChannel(channel);
      
              }else {
                  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),Notification_ID)
                          .setSmallIcon(R.mipmap.ic_launcher)
                          .setContentTitle(Title)
                          .setContentIntent(resultPendingIntent)
                          .setContentText(message)
                          .setStyle(inboxStyle)
                          .setSound(soundUri)
                          .setAutoCancel(true);
                  mNotificationManager.notify(Notification_ID, mBuilder.build());
              }
      

      【讨论】:

      • 虽然此代码 sn-p 可能是解决方案,但包含解释确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      【解决方案4】:

      需要在您的手机中启用自动启动

      我们无法直接启用 AutoStart。我们应该导航到那个屏幕

      try {
          Intent intent = new Intent();
          String manufacturer = android.os.Build.MANUFACTURER;
          if ("xiaomi".equalsIgnoreCase(manufacturer)) {
              intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
          } else if ("oppo".equalsIgnoreCase(manufacturer)) {
              intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
          } else if ("vivo".equalsIgnoreCase(manufacturer)) {
              intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
          }
      
          List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
          if  (list.size() > 0) {
              context.startActivity(intent);
          } 
      } catch (Exception e) {
          Crashlytics.logException(e);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多