如何创建在Android中点击时不会消失的通知?

int icon = R.drawable.icon4; CharSequence tickerText = "Hello"; // ticker-text long when = System.currentTimeMillis(); Context context = getApplicationContext(); CharSequence contentTitle = "Hello"; CharSequence contentText = "Hello"; Intent notificationIntent = new Intent(this, Example.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new Notification(icon, tickerText, when); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

这对我不起作用。

我怎么能创建一个可点击的通知并转到我的应用程序,但点击后不会消失?

你应该阅读整个事情而不只是一个部分,伙计。 请仔细阅读一步一步。

 // this String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); int icon = R.drawable.icon4; CharSequence tickerText = "Hello"; // ticker-text long when = System.currentTimeMillis(); Context context = getApplicationContext(); CharSequence contentTitle = "Hello"; CharSequence contentText = "Hello"; Intent notificationIntent = new Intent(this, Example.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new Notification(icon, tickerText, when); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); // and this private static final int HELLO_ID = 1; mNotificationManager.notify(HELLO_ID, notification); 
 int icon = R.drawable.ic_launcher; long when = System.currentTimeMillis(); NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Intent intent=new Intent(context,MainActivity.class); PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0); Notification notification; if (Build.VERSION.SDK_INT < 11) { notification = new Notification(icon, "Title", when); notification.setLatestEventInfo( context, "Title", "Text", pending); } else { notification = new Notification.Builder(context) .setContentTitle("Title") .setContentText( "Text").setSmallIcon(R.drawable.ic_launcher) .setContentIntent(pending).setWhen(when).setAutoCancel(true) .build(); } notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults |= Notification.DEFAULT_SOUND; nm.notify(0, notification); 

或者您可以从这里下载直接教程: http : //www.demoadda.com/demo/android/how-to-create-local-notification-notification-manager-demo-with-example-android-source-code_26

如果您使用的是Android 5.0>它变得更容易,function更改,但您可以使用相同的代码。

 //Some Vars public static final int NOTIFICATION_ID = 1; //this can be any int //Building the Notification NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.ic_stat_notification); builder.setContentTitle("BasicNotifications Sample"); builder.setContentText("Time to learn about notifications!"); NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, builder.build()); 

确保您处于应用程序上下文中,否则您可能需要传递上下文并更改源代码,如下所示

 NotificationCompat.Builder builder = new NotificationCompat.Builder(context); ... .. . NotificationManager notificationManager = (NotificationManager) context.getSystemService( context.NOTIFICATION_SERVICE); 

您可以在以下 url 查看完整源代码: https //github.com/googlesamples/android-BasicNotifications/blob/master/Application/src/main/java/com/example/android/basicnotifications/MainActivity.java#L73

这是代码

  Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Push Notification") .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); 

在这里,如果您想要在Android中点击时不会消失的通知? 所以设定

 setAutoCancel(false); 
  if (android.os.Build.VERSION.SDK_INT>16) { notificationManager.notify(5, notification.build()); }else { notificationManager.notify(5, notification.getNotification()); } 

要在android.os.Build.VERSION.SDK_INT<16工作,请记住做一些更改