我正在开发一个Android应用程序,我在其中触发使用Alarm服务的本地推送通知。第一次(早上6点)它工作正常,即使我从堆栈中删除一个应用程序,但我再次更改我的系统时钟(早上6点),它不会再次触发。我的代码如下所示,我也根据API级别进行通知。
// Activity where I am calling AlarmService
public class MainActivity extends FragmentActivity {
final static int RQS_1 = 4;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE,00);
calendar.add(Calendar.SECOND, 00);
// API LEVEL OREO or greater
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class); //ALARM IS SET
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}else{
// API LEVEL Lower than OREO
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),broadcast);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),broadcast);
}
}
}
警报接收器
public class AlarmReceiver extends BroadcastReceiver {
int importance = NotificationManager.IMPORTANCE_HIGH;
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, NotificationActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(notificationIntent);
localPushNotifications26GreaterAPI(context);
}
public void localPushNotifications26GreaterAPI(Context context){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
CharSequence name = "iam.peace";
String description = "Description";
NotificationChannel mChannel = new NotificationChannel(id, name,importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int notifyID = 1;
String CHANNEL_ID = "my_channel_01";
Notification notification = new Notification.Builder(context,CHANNEL_ID)
.setContentTitle("OREO LEVEL NOTIFICATION")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.notification_icon)
.setChannelId(CHANNEL_ID)
.build();
mNotificationManager.notify(notifyID, notification);
} else{
localPushNotifications26LowerAPI(context);
}
}
public void localPushNotifications26LowerAPI(Context context){
Intent intent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(context);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("I am Peace")
.setContentTitle("OREO Below LEVEL NOTIFICATION")
.setContentText("Oreo below level push notifications.")
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
}
我正在注册Receiver的清单文件:
<receiver android:name="helper.AlarmReceiver">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
答案 0 :(得分:1)
那是因为您正在使用setExtact方法进行一次性警报。您可以使用setRepeating进行重复警报(但由于API 19已不再准确):
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() ,60 * 60 * 24 * 1000, broadcast);
或在onReceiveMethod中的AlarmReceiver中创建新的一次性警报
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, NotificationActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(notificationIntent);
localPushNotifications26GreaterAPI(context);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),broadcast);
}
答案 1 :(得分:0)
来自Android orea,如果你选择了Jobschedular会更好,请参考google的官方文档, https://developer.android.com/about/versions/oreo/background.html