Background
My app gives event notifications to users. Notifications are sent by moderators. However app allows moderators to sent "Timed notifications" where they have option to sent notification for specific time ahead of time.
My logic
When moderators will send notifications, they will send specific tag. At client side, if tag is detected, device won't show notification. Instead it will use AlarmManager
and set alarm at given time. When intent is received from AlarmManager
at desired time, broadcast receiver will send notification.
Code
NotificationService.java
//Some other code
public void setTimedNotification(String notificationstring, int Seconds){
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add seconds to the calendar object
cal.add(Calendar.SECOND, Seconds);
Intent intent = new Intent(getBaseContext(), TimedNotifications.class);
PendingIntent sender = PendingIntent.getBroadcast(getBaseContext(), 1989, intent, PendingIntent.FLAG_UPDATE_CURRENT); //1989 is some random number
// Get the AlarmManager service
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
TimedNotifications.java
public class TimedNotifications extends BroadcastReceiver {
public static final int NOTIFICATION_ID = 1;
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int requestID = (int) System.currentTimeMillis();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, TargetClass.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int color = context.getResources().getColor(R.color.colorPrimary);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notificationTitle)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setColor(color)
.setContentText(notificationMessage).setAutoCancel(true);
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.XXX.ZZZ">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<permission
android:name="com.PackageName.App.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.PackageName.App.permission.C2D_MESSAGE" />
<application
<!-- Some other activities and details -->
<receiver
android:name="com.PackageName.App.gcm.TimedNotifications"
android:process=":remote"/>
<!-- Bla bla bla -->
</application>
</manifest>
Problem
This code works perfectly when device is running and screen is not locked. So I am sure service is working fine. It even gives me timed notification if device is not in sleep. This also tells that code is working fine. However is device is locked for more than 5-10 min. I don't get any notifications. I guess there is some permission problem. I have tried lot of combinations of permissions like by adding custom flag in intent , adding removing android:process=":remote"
, changing name of receiver class, including package name in receiver's name, checking spelling mistakes, I even tried using wakelock like shown in this post :/ etc. However no luck so far. Something is missing.