2017-04-21 49 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。应用程序需要保护在应用程序列表中。现在,我测试的至少两个解决方案工作正常。

感谢大家的帮助。我认为安迪和其他解决方案工作正常。但华为手机有这个特殊问题。