我做以下几点: 使用getNotification()
和你calandar.getTime()
作为参数scheduleNotification
private void scheduleNotification(Notification notification, Date alarmTime) {
Intent notificationIntent = new Intent(this, NotificationReceiver.class);
notificationIntent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationReceiver.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime.getTime(), pendingIntent);
}
private Notification getNotification() {
int colour = getNotificationColour();
Bitmap largeNotificationImage = getLargeNotificationImage();
return new RandomNotification(this).getNotification(
"Text",
"More text",
getNotificationImage(),
largeNotificationImage,
colour);
}
private int getNotificationColour() {
return ContextCompat.getColor(this, R.color.colorAccent);
}
private Bitmap getLargeNotificationImage() {
return BitmapFactory.decodeResource(this.getResources(),
R.mipmap.ic_launcher);
}
我RandomNotification类:
public class RandomNotification {
private Context context;
public RandomNotification(Context context) {
this.context = context;
}
public Notification getNotification(String title, String message, int imageResourceId, Bitmap largeResource, int colour) {
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(imageResourceId)
.setLargeIcon(largeResource)
.setTicker("Ticker")
.addAction(0, "Button", getButtonIntent())
.setSound(soundUri)
.setLights(Color.BLUE, 300, 100)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(getPendingNotificationIntent());
handleNotificationColor(builder, colour);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
return notification;
}
private Notification.Builder handleNotificationColor(Notification.Builder builder, int colour) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setColor(colour);
}
return builder;
}
private PendingIntent getPendingNotificationIntent() {
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context, 1,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingNotificationIntent;
}
private PendingIntent getButtonIntent() {
Intent notificationButtonReceiver = new Intent(context, NotificationButtonReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationButtonReceiver, PendingIntent.FLAG_CANCEL_CURRENT);
return pendingIntent;
}
}
我的通知按钮的接收机类:
public class NotificationButtonReceiver extends BroadcastReceiver {
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
// do something
}
}
我的更新通知接收器类
public class NotificationReceiver extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
}
我的清单文件:
<receiver
android:name=".NotificationButtonReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
<receiver
android:name=".NotificationReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
你可以参考[这里](https://stackoverflow.com/a/35124436/5860777)和[这一个] (https://stackoverflow.com/a/35127736/5860777)。这不是你的问题的答案,但你可以从它得到一些想法。你需要在你的代码中使用这个引用的一点点改变,它工作正常。 –
您确定转储不显示警报吗?我试了完全相同的代码,并在我的转储报警显示正确? – MPhil
难道这不是从一项活动中调用?我不得不传递上下文来执行此代码。 – JohnDoe