无法让状态栏通知工作......我按照本指南,并在中创建了以下代码AsyncTask
,它是从doInBackground()
方法调用的:
private void newNotification(int count) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(handler.getContext());
builder.setContentTitle("New Notification");
builder.setContentText("Testing notification");
builder.setNumber(count);
// Creates an explicit intent for an Activity in your app
Intent intent = new Intent(handler.getContext(), MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(handler.getContext());
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) handler.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
handler
是对 的引用MainActivity
,使用实现的接口,getContext()
它返回this.getContext()
MainActivity
出于某种原因,当我使用参数 1 调用它时,什么也没有发生。
@Override
protected String[] doInBackground(String... empty) {
newNotification(1);
return null;
}
调试显示所有代码行都在运行,但没有出现任何通知。我没有发现任何关于特殊权限的提及,或者它需要从任何地方的 UI 线程运行。我认为它不会因为后台服务旨在通知......就像消息到达时一样。
任何想法我做错了什么?