【问题标题】:how to open app on notification icon click?如何在点击通知图标时打开应用程序?
【发布时间】:2014-03-08 06:28:53
【问题描述】:

我知道可能是重复的,但我正在使用 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);
         }

        }

一切都很好,但是 单击该通知图标未打开我的应用程序`

【问题讨论】:

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


    【解决方案1】:

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

    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);
    

    【讨论】:

    • 通知说明 = 新通知已弃用朋友 :( 请修改我的代码 sn-p 我是 android 新手,这将很有帮助,谢谢
    【解决方案2】:

    我终于让它工作了,非常感谢大家

    @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);
    
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      相关资源
      最近更新 更多