当我从Recently Open
或Task Swipe
关闭我的应用时,我无法再收到通知。但当应用程序处于前台或后台时,我可以收到通知。我一直在寻找解决方案,但我找不到任何解决方案。我查看了应用信息,似乎是forced closed
。有没有办法解决这个问题?
更新 它似乎只发生在我的ASUS设备上,我在我的三星设备上进行了测试,但它确实有效。
这是我的代码。
清单
<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);