申请被关闭后,通知不会显示

时间:2014-12-18 21:25:51

标签: android notifications broadcastreceiver intentservice

我有一个较旧的应用程序(我们简单地称之为A)和一个较新的应用程序(我们称之为B)。

A有重复的AlarmManager,它将启动代码,显示来自app的随机文本的通知。即使您关闭应用程序(通过主页按钮和后退按钮)或从最近的应用程序列表中删除它也没有任何问题。

但是,有应用B.应用B的主要目的是在给定时间向用户显示一些通知。因此,一旦计划通知,就会有AlarmManager,它在给定时间显示通知。但问题来了。实际发生的是,大部分时间B都可以显示通知,但是当它被后退按钮关闭,从最近的应用程序列表中解除,或者Android在一定时间后关闭它时,AlarmManager调用BroadcastReceiver,它成功调用了IntentService(我知道因为它还会将调试消息发送到LogCat),但不会显示通知并且不会启动振动。

我的问题是:这个问题的原因是什么?或者有没有办法如何显示通知,即使应用程序没有打开,但仍然在后台运行(我的意思是,如果用户强制杀死应用程序,没有理由显示通知,但当用户只是以正常方式关闭它,通知应该显示)?

有应用程序A和B的代码。也许你将能够找出为什么A工作而B不能。

APP A

设置AlarmIntent:

    alarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getApplicationContext(), NotifReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);


    //Cancel previous alarm
    if(alarm != null){
        alarm.cancel(alarmIntent);
    }

    //Setup alarm
    alarm.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + AlarmManager.INTERVAL_HOUR, DAY_IN_MILLIS, alarmIntent);

广播接收器:

@Override
public void onReceive(Context context, Intent arg1) {
    Intent intent = new Intent(context, ReminderService.class);
    context.startService(intent);
}

IntentService:

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
    builder
        .setAutoCancel(true)
        .setContentTitle(getResources().getString(R.string.random_text).toString())
        .setContentText(text)
        .setSmallIcon(R.drawable.ic_notif_icon)
        .setLargeIcon( ( (BitmapDrawable) getResources().getDrawable(R.drawable.ic_launcher) ).getBitmap() )
        .setStyle(new NotificationCompat.BigTextStyle().bigText(text));

    //Intent for activity
    Intent fullNotifIntent = new Intent("android.intent.action.DISPLAY_TEXT");
    fullNotifIntent.putExtra("text", text);

    PendingIntent fullNotifPendingIntent = PendingIntent.getActivity(
            getApplicationContext(), 0, fullNotifIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(fullNotifPendingIntent);


    //Display notification
    Notification notif = builder.build();
    notif.flags = Notification.FLAG_AUTO_CANCEL;
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    manager.notify(UNIQUE_ID, notif);

APP B

设置AlarmIntent:

    AlarmManager alarm = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context.getApplicationContext(), NotifReceiver.class);
    intent.putExtra("notifId", meta.notifId);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, 
            PendingIntent.FLAG_UPDATE_CURRENT);


    //Cancel previous alarm
    if(alarm != null){
        alarm.cancel(alarmIntent);
    }

    alarm.set(AlarmManager.RTC_WAKEUP, meta.getRemindTime(), alarmIntent);

广播接收器:

@Override
public void onReceive(Context context, Intent receivedIntent) {
    Intent intent = new Intent(context, NotifService.class);
    long notifId= receivedIntent.getLongExtra("notifId", 0);
    intent.putExtra("notifId", notifId);
    context.startService(intent);

    Log.d("BroadcastReceiver", "Broadcast received!");
}

IntentService:

    Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
    if(vibrator.hasVibrator()){
        long pattern[] = {0, 500, 250};
        vibrator.vibrate(pattern, 2);
    }


    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
        .setAutoCancel(true)
        .setContentTitle(task.getName())
        .setContentText(task.getDescription())
        .setSmallIcon(R.drawable.ic_stat_notif)
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
        .setLights(config.getLedColor(), 2000, 2000);


    Notification notif = builder.build();
    android.app.NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    //Notify
    manager.notify(task.getNotifId(), notif);


    onLoadNextNotif();
    Log.d("serviceNotif", "done"); //This is successfully called even when displaying notification and vibrating fails

0 个答案:

没有答案