通知在发布后立即打开,不需要用户进行任何操作。
下面是我的代码:private void sendNotification(String message,String title,String id,String book) {
smLocalStore=new SMLocalStore(this);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Log.i("id:",id);
intent.putExtra("id",id);
intent.putExtra("val","1");
intent.putExtra("book",book);
int requestCode = 0;
//notification sending
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setSound(sound)
.setSmallIcon(R.mipmap.ic_lau)
.setLargeIcon(largeIcon)
.setContentTitle(title)
.setFullScreenIntent(pendingIntent,true)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(x, noBuilder.build()); //0 = ID of notification
}
从代码中删除以下行:
.setFullScreenIntent(pendingIntent,true)
Android Developer Documentation说:
setFullScreenIntent(PendingIntent intent, boolean highPriority)
启动的意图,而不是发布通知到状态栏。