点击通知后打开应用程序

我在我的应用程序中通知了以下代码:

//Notification Start notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon = R.drawable.n1; CharSequence tickerText = "Call Blocker"; long when = System.currentTimeMillis(); //now Notification notification = new Notification(icon, tickerText, when); Intent notificationIntent = new Intent(context, Main.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); Context context = getApplicationContext(); CharSequence title = "Call Blocker"; text = "Calls will be blocked while driving"; notification.setLatestEventInfo(context, title, text, contentIntent); notification.flags |= Notification.FLAG_ONGOING_EVENT; notification.flags |= Notification.FLAG_SHOW_LIGHTS; notificationManager.notify(1, notification); } 

我的通知触发得很好,但我的问题是,当我点击通知中心通知,它不会启动我的应用程序。

基本上,点击我的通知后没有任何反应! 我应该怎么做才能在点击我的通知后开始我的主要活动。 谢谢。

看下面的代码。 我正在使用它,它正在打开我的HomeActivity。

  NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, message, when); Intent notificationIntent = new Intent(context, HomeActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, title, message, intent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification); 

下面是使用NotificationCompact.Builder类的例子,这是最近的版本来build立通知。

 private void startNotification() { Log.i("NextActivity", "startNotification"); // Sets an ID for the notification int mNotificationId = 001; // Build Notification , setOngoing keeps the notification always in status bar NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ldb) .setContentTitle("Stop LDB") .setContentText("Click to stop LDB") .setOngoing(true); // Create pending intent, mention the Activity which needs to be //triggered when user clicks on notification(StopScript.class in this case) PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, StopScript.class), PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(contentIntent); // Gets an instance of the NotificationManager service NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Builds the notification and issues it. mNotifyMgr.notify(mNotificationId, mBuilder.build()); } 

请使用下面的代码完成简单通知的例子,在这个代码中你可以点击通知后打开应用程序,它会解决你的问题。

 public class AndroidNotifications extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button notificationButton = (Button) findViewById(R.id.notificationButton); notificationButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // Notification Title and Message Notification("Dipak Keshariya (Android Developer)", "This is Message from Dipak Keshariya (Android Developer)"); } }, 0); } }); } // Notification Function private void Notification(String notificationTitle, String notificationMessage) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); android.app.Notification notification = new android.app.Notification( R.drawable.ic_launcher, "Message from Dipak Keshariya! (Android Developer)", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, AndroidNotifications.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(AndroidNotifications.this, notificationTitle, notificationMessage, pendingIntent); notificationManager.notify(10001, notification); } } 

并参阅下面的链接了解更多信息。

简单的通知示例

看起来你错过了这部分,

 notification.contentIntent = pendingIntent; 

尝试添加这个,它应该工作。

用这个:

  Notification mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_music) .setContentTitle(songName).build(); mBuilder.contentIntent= PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); 

当点击通知时,contentIntent将负责打开活动

 public static void notifyUser(Activity activity, String header, String message) { NotificationManager notificationManager = (NotificationManager) activity .getSystemService(Activity.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent( activity.getApplicationContext(), YourActivityToLaunch.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity); stackBuilder.addParentStack(YourActivityToLaunch.class); stackBuilder.addNextIntent(notificationIntent); PendingIntent pIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new Notification.Builder(activity) .setContentTitle(header) .setContentText(message) .setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) .setContentIntent(pIntent).setAutoCancel(true) .setSmallIcon(drawable.notification_icon).build(); notificationManager.notify(2, notification); } 

使用下面的代码为打开的活动创build通知。 它适用于我。 完整的代码

  Intent myIntent = new Intent(context, DoSomething.class); PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK); myNotification = new NotificationCompat.Builder(context) .setContentTitle("Exercise of Notification!") .setContentText("Do Something...") .setTicker("Notification!") .setWhen(System.currentTimeMillis()) .setContentIntent(pendingIntent) .setDefaults(Notification.DEFAULT_SOUND) .setAutoCancel(true) .setSmallIcon(R.drawable.ic_launcher) .build(); notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(MY_NOTIFICATION_ID, myNotification); 

用我的例子…

  public void createNotification() { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon, "message", System.currentTimeMillis()); // Hide the notification after its selected notification.flags |= Notification.FLAG_AUTO_CANCEL; Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); long[] pattern = { 0, 100, 600, 100, 700}; vibrator.vibrate(pattern, -1); Intent intent = new Intent(this, Main.class); PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); String sms = getSharedPreferences("SMSPREF", MODE_PRIVATE).getString("incoming", "EMPTY"); notification.setLatestEventInfo(this, "message" , sms, activity); notification.number += 1; notificationManager.notify(0, notification); }