BroadcastReceiver cannot show notification when app closed/killed

断了今生、忘了曾经 提交于 2020-01-04 05:17:15

问题


My task is to set a alarm to trigger receiver to show notification a specific time.

And the code works perfectly if app is opened or just in background.

At the time app is being killed, receiver can still be triggered when alarm is executed.

However, no notification was shown.

What can I do?

Alarm function:

public static void makeAlarm(Context context,Date date) {
    Log.i("Alarm", "makeAlarm");
    AlarmManager alarmMgr;
    PendingIntent alarmIntent;
    alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    alarmMgr.set(AlarmManager.RTC_WAKEUP,
            date.getTime(), alarmIntent);
}

AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {
    public AlarmReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
                Log.i("Alarm", "onReceive");
                android.support.v4.app.NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(context)
                                .setSmallIcon(R.drawable.app_logo)
                                .setContentTitle("Title")
                                .setContentText("Message");

                Intent resultIntent = new Intent(context, MainActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addParentStack(MainActivity.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent =
                        stackBuilder.getPendingIntent(
                                0,
                                PendingIntent.FLAG_UPDATE_CURRENT
                        );
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager =
                        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                Random rand = new Random();
                mNotificationManager.notify(rand.nextInt(), mBuilder.build());

    }
}

Manifest.xml

<receiver
    android:name=".receiver.AlarmReceiver"
    android:enabled="true"
    android:exported="true" />

回答1:


Please run the sample code. I think it works as you want.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setUpAlarm(10 * 1000);
}
public void setUpAlarm(long triggerTimeInMS) {
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1,  intent, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+triggerTimeInMS , pendingIntent);
}
}

Create the receiver class -

public class AlarmReceiver extends BroadcastReceiver {
private String TAG = "AlarmLog";

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "Incoming Call Received", Toast.LENGTH_LONG).show();

    showNotification(context);

}

private void showNotification(Context context) {
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.alarm)
            .setContentTitle("Notification!") // title for notification
            .setContentText("Alarm Received")
            .setSound(soundUri)// message for notification
            .setAutoCancel(true); // clear notification after click
        NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
}
}

don't forget tor register the receiver . inside manifest.xml put the below code-

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name="com.example.shajib.alarmtest.AlarmReceiver" />
</application>



回答2:


When you set the alarm using -

alarmMgr.set(AlarmManager.RTC_WAKEUP,date.getTime(), alarmIntent);

I think you date.getTIme() is retrieving the current time. So when the alarm is tried to execute, the triggered time will already passed. Better, you can do some delay(say, five seconds) for triggering your alarm. Just use the line -

alarmMgr.set(AlarmManager.RTC_WAKEUP,date.getTime() + 5*1000, alarmIntent);


来源:https://stackoverflow.com/questions/37179957/broadcastreceiver-cannot-show-notification-when-app-closed-killed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!