Receive notification when app is closed

2019-08-03 05:00发布

问题:

I try to implement system to receive push notifications on my application. I use Firebase and everything is ok when the app is active or in background but nothing happen when the app is closed. I tried to create a WakefullBroadcastReceiver like that :

public class NotificationReceiver extends WakefulBroadcastReceiver {

    public static final String action = "com.myapp.notification.RECEIVE";
    private static final String KEY_PUSH_DATA = "com.parse.Data";

    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        switch (intentAction) {
            case action:
                String pushDataStr = intent.getStringExtra(KEY_PUSH_DATA);
                if (pushDataStr == null) {
                    return;
                }
                Log.e("PUSH", "Push data : "+pushDataStr);
                Bundle extras = intent.getExtras();
                MyappNotificationManager.getInstance().parseBundle(extras);
                break;
        }
    }
}

and I add this in Manifest.xml

<receiver android:name=".notification.NotificationReceiver">
    <intent-filter>
        <action android:name="com.myapp.notification.RECEIVE"/>
    </intent-filter>
</receiver>

It doesn't work and I can't find documentation on Android developper website.

Thanks for your help.

回答1:

add to Manifest.xml :

    <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application>
    <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED"></action>
                </intent-filter>
            </receiver>
            <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" /> 
</application>