When I closes my app from Recently Open or Task Swipe I can no longer receives notifications. But when app is in foreground or background I can receives notifications. I've been looking for solutions but I can't find any solutions.I checked the app information and it seems it is forced closed. Is there anyway way to solve this?
Update It seems that it only happens in my ASUS devices, I tested it in my Samsung devices and it works.
Here's my code.
Manifest
<receiver
android:name="com.my.package.utils.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.my.package" />
</intent-filter>
</receiver>
<service android:name="com.my.package.utils.GCMIntentService"
android:enabled="true"/>
GcmBroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
Log.e("Test", "onReceive");
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GCMIntentService
public class GCMIntentService extends IntentService {
public boolean isIntentServiceRunning = false;
private String id;
Notify notifObj;
Message messageObj;
String notifType = "";
public GCMIntentService() {
super("GCMIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
if(!isIntentServiceRunning) {
isIntentServiceRunning = true;
Log.i("Notif", "GCM RECEIVER");
extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
Log.i(TAG, gcm.getMessageType(intent));
id = extras.getString("id");
todayCal = Calendar.getInstance();
context = this;
try{
notification_id = Integer.parseInt(id);
} catch (Exception e) {
Log.e(TAG, "" + e.toString());
}
jsonParser = new JSONParser(context);
new AsyncAccessNotification().execute();
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
keywordList = mySQLiteAdapter.getKeywords();
categoryIdList = mySQLiteAdapter
.getEnabledMerchantCategoriesWithFiltering();
mySQLiteAdapter.close();
strJson = extras.getString("notification");
notifObj = jsonParser.parseJSONNotification(strJson);
messageObj = jsonParser.parseJSONNotificationIntoMessages(strJson);
Log.e(TAG, "" + strJson);
messageObj.getType();
messageObj.getDate();
messageObj.getNotification_id();
messageObj.getMessage();
Log.e("", "MSG DATE: " + messageObj.getDate());
messageObj.getSubject();
messageObj.getReference_id();
// insert into Messages
mySQLiteAdapter.openToRead();
mySQLiteAdapter.insertNotificationsIntoMessages(messageObj);
mySQLiteAdapter.close();
if (Utils.getGeneralAnnNotificationToggles(this) == 1) {
try {
Log.e(TAG, "" + notifObj.getType());
} catch (NullPointerException e) {
}
sendNotification(notifObj.getMessage(),
notifObj.getReference_id(),
notifObj.getId()); // ONE
}
}
message = notifObj.getMessage();
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private int getNotificationIcon() {
boolean useSilhouette = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
return useSilhouette ? R.drawable.icon_notif : R.drawable.ic_launcher;
}
// General Notification
private void sendNotification(String msg, int merchant_id, int id) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent;
try {
if (notifObj.getType().equalsIgnoreCase("ONE")) {
Log.e(TAG, "Notif Type: " + notifObj.getType());
Log.i(TAG, "extra merchant: " + merchant_id);
Intent resultIntent = new Intent(this, MerchantDetailsActivity.class);
resultIntent.putExtra(Constants.MERCHANT_ID, merchant_id + "");
resultIntent.putExtra(Constants.CATEGORY_TEXT, "Notifications");
Log.e("WITH_MERCHANT", "Notification with merchant: "+merchant_id);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(MerchantDetailsActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setLargeIcon(largeIcon())
.setSmallIcon(getNotificationIcon())
.setContentTitle("BDO Deals")
.setStyle(
new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(resultPendingIntent);
Notification notif = mBuilder.build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(id, notif);