以下代码不起作用。也可以尝试这个而不是 getapplicationcontext。我需要提示:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder nb = new NotificationCompat.Builder();
nb.setContentTitle("title");
nb.setContentText("message");
nb.setSmallIcon(R.drawable.ic_launcher);
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
final Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
nb.setContentIntent(contentIntent);
Notification notification = nb.getNotification();
nm.notify(0, notification);
}
}
如何在 onclick 后删除通知?
更新: 我解决了一个错误。但还有一个错误:
最佳答案
将 getSystemService 替换为 context.getSystemService!
关于java - Android 警报管理器和通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13032259/