我知道已经提出了几个类似的问题,但答案都包括已弃用的解决方案。
我已经开始在android上开发一个应用程序,它显示一些关于通知的信息(始终保持开启状态),我希望用户能够通过点击通知来访问应用程序。
以下是我所做的工作,当我点击通知(显示正确)时没有任何操作
我有一个名为SendNotification的方法,它由按键调用,除了调用
之外几乎没有private void DispatchNotification(String numValue,String nameValue){
Intent intent=new Intent(this,MainActivity.class);
PendingIntent pIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
builder.setAutoCancel(false);
builder.setContentTitle("Just Title");
builder.setContentText(nameValue+" "+numValue);
builder.setSmallIcon(R.drawable.abc_popup_background_mtrl_mult);
Notification notification = builder.build();
notification.flags|= Notification.FLAG_ONGOING_EVENT;
builder.addAction(R.drawable.abc_popup_background_mtrl_mult,"MENU",null);
builder.setContentIntent(pIntent);
NotificationManager manager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
manager.notify(8,notification);
}
提前致谢。
答案 0 :(得分:1)
根据documentation,您可以通过打开活动来构建通知:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, ResultActivity.class);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mBuilder.setOngoing(true);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
答案 1 :(得分:1)
第1步:摆脱现有的builder.build()
电话。
第2步:将notification.flags|= Notification.FLAG_ONGOING_EVENT;
替换为builder.setOngoing(true);
。
第3步:将manager.notify(8,notification)
替换为manager.notify(8, builder.build());
I,在完成Notification
之前,您正在构建Builder
。
答案 2 :(得分:0)
试试这个:
Intent intent = new Intent(ctx, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK );
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT|PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.icon_notification)
.setContentTitle("Your app name")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());