0

My application require future notification on different time. So I implemented below code with help of this link.

But at the end my conclusion is this code doesn't fire all notification, some times none of the future notification will fire.

Application is client-server base app so date-time receive from server. example is as below.

SimpleDateFormat simpleDateFormatDateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.US);
Date eventAlertDateTime = simpleDateFormatDateTime.parse("2016-09-08 16:41:00.0");
Calendar calendar = Calendar.getInstance();
calendar.setTime(eventAlertDateTime);
setInternalBroadcastNotification(eventAlertDateTime, "Your brunch is readyon 2nd floor.");
eventAlertDateTime = simpleDateFormatDateTime.parse("2016-09-09 16:41:00.0");
calendar.setTime(eventAlertDateTime);
setInternalBroadcastNotification(eventAlertDateTime, "Your brunch is readyon 2nd floor.");

public void setInternalBroadcastNotification(Date eventAlertDateTime, String eventNotificationMessage) {
try {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(eventAlertDateTime);

    long timeDifference = calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
    if (timeDifference > 0) {
        Intent receiverServiceIntent = new Intent(mContext, InternalBroadcastReceiver.class);
        receiverServiceIntent.putExtra("eventNotificationMessage", eventNotificationMessage);

        PendingIntent pendingIntent = PendingIntent.getService(mContext, (int) eventAlertDateTime.getTime(), receiverServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }
    }
} catch (Exception exception) {
    exception.printStackTrace();
}
}

public class InternalBroadcastReceiver extends Service {

public InternalBroadcastReceiver() {
}
public static final int NOTIFICATION_ID = 1;
public class ServiceBinder extends Binder {
    InternalBroadcastReceiver getService() {
        return InternalBroadcastReceiver.this;
    }
}
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    showNotification(intent.getStringExtra("eventNotificationMessage"));
    return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}
private final IBinder mBinder = new ServiceBinder();
private void showNotification(String message) {
    NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    //DO NOTHING ON NOTIFICATION
    Intent callingIntent = new Intent();

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, callingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    //FROM LOLLIPOP SmallIcon MUST BE WHITE
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notificationBuilder.setSmallIcon(R.drawable.ic_notification);
    } else {
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    }
    notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
            .setTicker(getString(getApplicationInfo().labelRes))
            .setContentTitle(getResources().getString(getApplicationInfo().labelRes))
            .setContentText(message)
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentIntent(contentIntent);

    mNotificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    // Stop the service when we are finished
    stopSelf();
}
}

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<service
android:name=".utils.InternalBroadcastReceiver"
android:enabled="true"
android:exported="true"></service>
Manish Jain
  • 842
  • 1
  • 11
  • 29

1 Answers1

1

Change

PendingIntent pendingIntent = PendingIntent.getService(mContext, (int) eventAlertDateTime.getTime(), receiverServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

To

Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
PendingIntent pendingIntent = PendingIntent.getService(mContext, n , receiverServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

And

Pass your future time like this :

alarmManager.set(AlarmManager.RTC_WAKEUP, eventAlertDateTime.getTime(), pendingIntent);
Nisarg
  • 1,358
  • 14
  • 30
  • I tried your way for changing PendingIntent.getBroadcast and alarmManager.set, but when app is offline or in closed state notification not fire. – Manish Jain Sep 08 '16 at 14:29
  • @ManishJain Ok try [this](http://stackoverflow.com/a/33261240/3117966) out ,In your case use getService because you're handling using service. – Nisarg Sep 09 '16 at 04:40
  • Thanks for your response, but as you can refer in my code, I code such in a way for new api and old api for alarmManager – Manish Jain Sep 09 '16 at 05:38