我试图在我的应用中显示通知。一切似乎都没问题,但在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());
}
}
答案 0 :(得分:0)
使用com.google.android.gms库:play-services-gcm' 9.6.0或更高版本问题解决了。我使用的是GCM,而不是FCM。
答案 1 :(得分:0)
我也遇到了同样的问题,然后我找到了解决方案,您需要从电池优化列表中删除您的应用程序,并标记您的应用程序未优化,我在 onePlus5 ,在终止应用程序后,通知仍然有效。
步骤:
现在我发现了,所以现在我需要看看如何使用代码来做到这一点。 请让我知道您是否遇到相同的问题,否则它将解决您的问题。 :
答案 2 :(得分:0)
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());
}
答案 3 :(得分:0)
需要在手机中启用自动启动
我们无法直接启用自动启动。我们应该导航到该屏幕
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);
}