I made a service for receiving notification, every time data is updated at the back end. Here's the code for the service:
public class FeedbackService extends IntentService {
public FeedbackService() {
super("FeedbackService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("MyService", "About to execute feedback call");
feedbackCheckCall(this);
}
private void feedbackCheckCall(final Context context){
//Call for getting checking data from backend.
}
private void sendNotification(Context context) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_icon)
.setContentTitle("Feedback Reply")
.setContentText("You've a reply waiting for your feedback!")
.setVibrate(new long[]{500,500,500});
Intent notificationIntent = new Intent(context, navHomeActivity.class );
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
Here's the code for the reciever :
public class FeedbackRecieiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent dailyUpdater = new Intent(context, FeedbackService.class);
context.startService(dailyUpdater);
Log.d("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive");
}
}
Here's the code from where I call it:
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY,0);
updateTime.set(Calendar.MINUTE, 0);
updateTime.set(Calendar.SECOND, 0);
long intervalTime = 2*60*60*1000; //in milliseconds format is : h*m*s*1000
Intent intent = new Intent(context, FeedbackRecieiver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,updateTime.getTimeInMillis(),intervalTime,pendingIntent);
My manifest declarations are :
<service android:name=".navFragments.feedbackSuppport.FeedbackService"/>
<receiver android:name=".navFragments.feedbackSuppport.FeedbackRecieiver"/>
The app is crashing when I use a signed copy with this error on startup:
Sending non-protected broadcast com.motorola.motocare.INTENT_TRIGGER from system 6836:com.motorola.process.system/1000 pkg com.motorola.motgeofencesvc
java.lang.Throwable
at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:18179)
at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:18779)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:512)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2905)
at android.os.Binder.execTransact(Binder.java:565)
Can someone please help me out with this? I'm really stuck here. Thanks in advance.
So, there was no issue with broadcast receiver or the service. The issue was while using proguard. I was not using it correctly. I had disabled it in the debug variant and enabled it in the release variant. The app was crashing due to that.