Maxime
Maxime

Reputation: 1392

Receive notification when app is closed

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.

Upvotes: 1

Views: 1137

Answers (2)

Mohammed Javad
Mohammed Javad

Reputation: 696

For some device models of manufacture's like Xiaomi, Redmi, Letv, Honor, Oppo, Vivo etc you need to get the AutoStart permission to make this work when the app is closed. And taking this permission differs from manufacturer to manufacturer where the popular Apps are already provided with AutoStart permission by the Manufacturer itself. So you could use an Android library called AutoStarter for taking permission from such devices.

Upvotes: 0

H.Sattar
H.Sattar

Reputation: 11

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> 

Upvotes: 1

Related Questions