android - 本地通知在 Android 8.0 Oreo 中不起作用
问题描述
我在我的应用程序中使用了本地通知。用户每天都会收到通知。它在 8.0 以下版本中工作正常,但在 8.0 中没有。这是我的代码。
MainActivity.Here 我正在调用 Service。
Intent sync = new Intent(ctx,NotificationService.class);
startService(sync);
NotificationService.class.OnCreate 方法
notificationManager = new SipNotifications(this);
notificationManager.onServiceCreate();
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
//cal.add(Calendar.HOUR, 24);
cal.add(Calendar.HOUR_OF_DAY,24);
am.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
Alarm_Receiver 类 onReceive 方法
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
System.out.println("Alarm Receiver Notification"+"In Notification");
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/* Create or update. */
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, context.getResources().getString(R.string.app_name),
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notification = builder.setContentTitle("coins.")
.setContentText("Your coins are ready to collect.")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setChannelId(CHANNEL_ID)
.setContentIntent(pendingIntent).build();
}else {
notification = builder.setContentTitle("coins.")
.setContentText("Your coins are ready to collect.")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pendingIntent).build();
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
解决方案
尝试这个
public class NotificationHelper {
private Context mContext;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder mBuilder;
public static final String NOTIFICATION_CHANNEL_ID = "10001";
public NotificationHelper(Context context) {
mContext = context;
}
/**
* Create and push the notification
*/
public void createNotification(String title, String message)
{
/**Creates an explicit intent for an Activity in your app**/
Intent resultIntent = new Intent(mContext , SomeOtherActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}
}
在您创建通知的课程上
notificationHelper=new NotificationHelper(this);
Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notificationHelper. createNotification("Title","Message");
}
});
推荐阅读
- python - 查找最接近的元素值(最小值和最大值)以及差异百分比
- gradle - 在从包含三个 .gradle 文件的存储库创建自定义 gradle 插件时需要帮助
- javascript - Owl Carousel 2:添加标题,单击并相应地移动到项目
- hive - Apache Ranger 中基于标签的策略不起作用
- python - ImportError:无法从“tensorflow.python.profiler”导入名称“trace”
- c# - KeyPerFileConfigurationSource 没有同时设置目录路径和忽略条件的选项
- node.js - 如何更新 AWS Lambda index.js 文件
- javascript - Formdata 不会将数字输入字段转换为字符串
- leaflet - 在 QGIS 中,qgis2web 插件是否支持点图层的热图符号系统?
- amazon-web-services - K8s Pods 失败:为 pod“app”运行“VolumeBinding”预绑定插件时出错:绑定卷失败:等待条件超时