Receive notification when app is closed

倾然丶 夕夏残阳落幕 提交于 2019-12-11 05:59:53

问题


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> 


来源:https://stackoverflow.com/questions/39897053/receive-notification-when-app-is-closed

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