如何打开通知图标上的应用程序点击?

时间:2014-03-08 06:28:54

标签: android performance android-intent notifications android-webview

  

我知道可能重复,但是我在几个间隔之后使用AlarmManager来获取通知

我使用以下代码来显示通知

public class MainActivity extends Activity {
 public void setRepeatingAlarm() {
          Intent intent = new Intent(this, TimeAlarm.class);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
          am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
            (50 * 1000), pendingIntent);
         }
}

    public class TimeAlarm extends BroadcastReceiver {

        NotificationManager nm;

         @Override
         public void onReceive(Context context, Intent intent) {
          nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
          PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(), 0);
          Notification noti = new NotificationCompat.Builder(context)
          .setSmallIcon(R.drawable.ic_launcher)
          .setTicker("ticker")
          .setWhen(System.currentTimeMillis()+50000)
          .setContentTitle("title")
          .setContentText("Check out ")
          .setContentIntent(contentIntent)

          //At most three action buttons can be added
          .setAutoCancel(true).build();
    int notifyID =1;
    nm.notify(notifyID, noti);
         }

        }
  

一切都很好,但是   点击该通知图标不打开我的APP`

2 个答案:

答案 0 :(得分:2)

看看我使用的代码,它对我来说非常适合。

Long rowId = intent.getExtras().getLong(TaskDatabase.KEY_ROWID);
    String a = intent.getExtras().getString(TaskDatabase.KEY_TITLE);

    int count = 0; 
    int i = 1;

    String body = "Task needs your attention.";
    String title = "Reminder";

    NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, Main.class); // Change it any activity, you want it to open. 
    notificationIntent.putExtra(TaskDatabase.KEY_ROWID, rowId);

    PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification note = new Notification(R.drawable.white, body, System.currentTimeMillis());
    note.setLatestEventInfo(this, a, body, pi);



        SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String ringPreference = preference.getString("Ringtone", "DEFAULT_SOUND");

        note.defaults |= Notification.DEFAULT_SOUND;
        note.flags  |= Notification.FLAG_AUTO_CANCEL;
        note.sound = Uri.parse(ringPreference);

        int id = (int)((long)rowId);
        mgr.notify(id, note);

答案 1 :(得分:0)

最后,我现在感谢它的工作非常感谢

@Override
     public void onReceive(Context context, Intent intent) {

         Intent notificationIntent = new Intent(context, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

             NotificationManager notificationManager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);

             Notification noti = new NotificationCompat.Builder(context)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setTicker("ticker")
                               //.setLargeIcon(largeIcon)
                               //.setWhen(System.currentTimeMillis()+1000)
                                .setContentTitle("title")
                                .setContentText("message!")
                                .setContentIntent(contentIntent)
                                //At most three action buttons can be added
                                .setAutoCancel(true).build();
            int notifyID =0;
            notificationManager.notify(notifyID, noti);

     }