1

我正在嘗試執行預定通知。所有作品除外:當應用程序處於活動狀態並最小化時。通知自動啓動活動,無需等待用戶點擊它。狀態欄通知自動啓動活動

在reveive:

public void onReceive(Context context, Intent paramIntent) { 

     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 


     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.cancelAll(); 
     Notification notification = new Notification(R.drawable.logo_f, context.getResources().getString(R.string.notification_text), System.currentTimeMillis()); 

     Intent notificationIntent = new Intent(context, TimeLeftActivity.class); 

     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent intent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 

     notification.setLatestEventInfo(context, context.getResources().getString(R.string.notification_text), "", intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.sound=alarmSound; 
     // Fire the notification 
     notificationManager.notify(1, notification); 


    } 

我開始通知方法:基里爾回答後

private void createScheduledNotification(int sec) 
    { 
     // Get new calendar object and set the date to now 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     // Add defined amount of days to the date 
     calendar.add(Calendar.SECOND, sec); 

     // Retrieve alarm manager from the system 
     AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE); 

     // Every scheduled intent needs a different ID, else it is just executed once 
     int id = 1; 

     // Prepare the intent which should be launched at the date 
     Intent intent = new Intent(this, TimeAlarm.class); 

     // Prepare the pending intent 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     alarmManager.cancel(pendingIntent); 
     // Register the alert in the system. You have the option to define if the device has to wake up on the alert or not 
     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
    } 

編輯。錯誤仍然存​​在。通知自動啓動等待意圖,不等待點擊。

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

     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 


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


     Intent notificationIntent = new Intent(context, TimeLeftActivity.class); 

     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent intent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 
     Notification notification = new NotificationCompat.Builder(context) 
       .setContentTitle(context.getResources().getString(R.string.notification_text)) 
       .setContentIntent(intent) 
       .setSound(alarmSound) 
       .build(); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Fire the notification 
     notificationManager.notify(1, notification); 


    } 
+0

'的PendingIntent意圖= PendingIntent.getActivity(上下文,0, notificationIntent,0);'什麼大約爲零?第二個0應該是PendingIntent的常量 – 2015-02-05 20:01:23

回答

1

這是很難找到的錯誤,因爲你使用過時的API在你的代碼,你應該使用Notication.Builder

Notification noti = new Notification.Builder(mContext) 
    .setContentTitle("New mail from " + sender.toString()) 
    .setContentText(subject) 
    .setSmallIcon(R.drawable.new_mail) 
    .setLargeIcon(aBitmap) 
    .build(); 

如果您需要支持舊版本,你可以使用NotificationCompat

UPDATE

這是來自我的應用程序的示例,它會引發通知,其中打開activit y通過點擊,我標記方法添加意圖。

String message = context.getString(R.string.notif_message); 
    Intent notificationIntent = new Intent(AddBpRecordActivity.ADD_ACTION); 

    NotificationCompat.Builder nb = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ic_notif_logo) 
      .setContentTitle(message) 
      .setContentText(billet.comment) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .setAutoCancel(true) 
     >>> .setContentIntent(PendingIntent.getActivity(context, (int) billet.id, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT)) 
      .setWhen(System.currentTimeMillis()); 
    Notification notification = nb.build(); 

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify((int) billet.id, notification); 
+0

如何設置它的意圖?所以舔它會打開活動?設置內容意圖? – sanevys 2015-02-05 19:48:28

+0

@sanevys我更新了答案 – 2015-02-05 19:55:17

+0

仍然自動打開活動,可能是手機漏洞? – sanevys 2015-02-05 20:07:24