In my app, this is how notifications should work:
Now, the issue is that the notifications are not seen on all phones. In fact, it is only being seen on a handful of phones. Furthermore, some phones show erratic behavior - sometimes showing it and other times not showing it.
Can someone please help me here?
EDIT: There is an AlarmManager which is setup and it calls this:
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
SimpleAlarmManager.INTERVAL_DAY,
PendingIntent.getBroadcast(context, id,
new Intent(context, AlarmReceiver.class),
PendingIntent.FLAG_NO_CREATE);
AlarmReceiver.java:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context arg0, final Intent arg1) {
Intent background = new Intent(arg0, CallNotificationAfter1sec.class);
arg0.startService(background);
}
}
CallNotificationAfter1sec.java:
public class CallNotificationAfter1sec extends Service {
Context context;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand( final Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)...
}
}, 1000);
stopSelf();
return START_STICKY;
}
@Override
public void onCreate() {
this.context = this;
}
@Override
public void onDestroy() {
}
}
答案 0 :(得分:0)
If the device is sleeping when the AlarmReceiver gets fired, it's only guaranteed to wake up for the onReceive() call of the AlarmReceiver, and it might go back to sleep instantly after that call completes.
Starting a service is an asynchronous call, so the phone might have gone back to sleep before starting the service.
Note the common use of the word "might", as the time the device is awake can be very different from time to time, and phone to phone.
To fix your issue, you need to hold a wakelock. Basically you manually ask the phone to stay awake untill you tell it to go to sleep again. But be very careful of this, and be sure to tell it to sleep again, or you'll be draining ALOT of battery.
I used the following code in one of my old projects to do exactly this:
Manifest:
checkBox.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
checkBox.setChecked(!checkBox.isChecked());
...
return true;
}
});
Helper class:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
That should be able to fix all your problems.
Hope you get it to work.
答案 1 :(得分:0)
As documented, you need to set the following for a notification to work. Are you setting them all?
Required notification contents
A Notification object must contain the following: