2017-04-21 113 views
0

我正在嘗試使用推送通知提醒用戶。每天早上提醒用戶的Android應用程序

我一直在使用AlarmManager,BroadcastReceiver和IntentService。

在下面的代碼中,我以60秒的間隔測試了我的AlarmManager。

問題: 一切工作正常,直到我關閉我的應用程序,警報不再發射。

任何想法下一步去哪裏?

清單:

<service 
    android:name=".IntentMentor" 
    android:exported="false" > 
</service> 

<receiver android:name=".AlertReceiver" 
    android:process=":remote"> 
</receiver> 

MainActivity:

Intent intent = new Intent(getApplicationContext(), AlertReceiver.class); 

final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, 
    intent, PendingIntent.FLAG_UPDATE_CURRENT); 

long firstMillis = System.currentTimeMillis(); 
AlarmManager alarm = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE); 
// 1s is only for testing 
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 1000*60, pIntent); 

接收機:

public class AlertReceiver extends BroadcastReceiver { 

    private static final String TAG = "MyReceiver"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "MyReceiver on receive"); 
     Intent i = new Intent(context, IntentMentor.class); 
     context.startService(i); 
    } 
} 

IntentService:

public class IntentMentor extends IntentService { 

    NotificationManager notificationManager; 
    int notifID = 33; 

    private static final String TAG = "MonitorService"; 

    public IntentMentor() { 
     super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Log.d("TAG", "Service method was fired."); 
     pushNotification(); 
    } 

    public void pushNotification(){ 
     NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this); 
     notificBuilder.setContentTitle("test"); 
     notificBuilder.setContentText("this is text"); 
     notificBuilder.setTicker("this is Ticker?"); 
     notificBuilder.setSmallIcon(R.drawable.ic_info_black_24dp); 
     notificBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND); 

     Intent intent = new Intent(this, Card.class); 

     TaskStackBuilder tStackBuilder = TaskStackBuilder.create(this); 
     tStackBuilder.addParentStack(Card.class); 
     tStackBuilder.addNextIntent(intent); 

     PendingIntent pendingIntent = tStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); 

     notificBuilder.setContentIntent(pendingIntent); 

     notificationManager = (NotificationManager) getSystemService((Context.NOTIFICATION_SERVICE)); 
     notificationManager.notify(notifID, notificBuilder.build()); 
    } 
} 
+0

[嘗試,這可能是這個幫助你](http://stackoverflow.com/questions/35121191/i-want-show-notification-at-800-am-everyday/35127736#35127736) –

+0

我試圖解決方案在你的鏈接後面。謝謝..但我仍然有同樣的問題。當應用程序未打開時,警報不會觸發。 – Wiltson

+0

請確保您授予WAKE_LOCK的權限 –

回答

0

試試這個我更新你的主要活動代碼。現在試試這個。它對我來說工作得很好。

Intent intent = new Intent(getApplicationContext(), AlertReceiver.class); 

final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, 
     intent, PendingIntent.FLAG_UPDATE_CURRENT); 

long firstMillis = System.currentTimeMillis(); 
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 

if (android.os.Build.VERSION.SDK_INT >= 23) 
{ 
    alarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
      firstMillis, 1000*60, pIntent); 
} 
else if (android.os.Build.VERSION.SDK_INT >= 19 
     && android.os.Build.VERSION.SDK_INT < 23) 
{ 
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
      firstMillis, 1000*60, pIntent); 
} 
else 
{ 
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, 
      firstMillis, 1000*60, pIntent); 
} 
+0

如果應用程序關閉,警報管理器是否工作? – Dejvid

+0

是的,這對我有用。 –

+0

出於某種原因報警。setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstMillis,1000 * 60,pIntent);對我不起作用。 setAndAllowWhileIdle只需要3個參數?我只是如此迷茫... – Wiltson

0

最後。經過十個小時的測試,失敗,測試,失敗..我傷口的答案。

Protected Apps in Huawei Phones

所以你看,問題是我的華爲手機P8。應用程序需要保護在應用程序列表中。現在,我測試的至少兩個解決方案工作正常。

感謝大家的幫助。我認爲安迪和其他解決方案工作正常。但華爲手機有這個特殊問題。