React-native-onesignal: get notification in background(android) when app is closed

Created on 22 May 2018  路  8Comments  路  Source: OneSignal/react-native-onesignal

I'm trying to get notification in background in android when app is closed and I don't get. I get only when I'm into the app(for now, it crash me I opened another issue).

export const sendNotificationOneSignal = (heading="",content,users_playerIds) => {
    console.log('users_playerIds', users_playerIds)
    let message = {
        app_id: ONESIGNAL_APPID,
        contents: { "en": content},
        headings:{"en":heading},
        include_player_ids: users_playerIds,
        content_available: true
    };

    var headers = {
        "Content-Type": "application/json; charset=utf-8",
        "Authorization": `Basic ${ONESIGNAL_AUTHKEY}`
    };


    var options = {
        uri: "https://onesignal.com/api/v1/notifications",
        method: "POST",
        headers: headers,
        body: message,
        json: true
    }


     return rp(options)


}

Most helpful comment

@Nightsd01 I declared android notification extender service as described in tutorial and I still not get the notification when app is close.
I did the three steps
image

should I need to do that also?
image

All 8 comments

This is expected. The received event only fires if the app is actually open (either open in foreground or in background). If it's terminated, the received event won't fire. If you want background notification events you'll have to implement the Android Notification Extender Service

Can i do it with react native? Why in ios it works fine with react-native-onesignal?

@Nightsd01 I declared android notification extender service as described in tutorial and I still not get the notification when app is close.
I did the three steps
image

should I need to do that also?
image

@adirzoari did you get solution to this? couldn't solve it for days now

@Mixpeal yes. what phone do you have? some phones like xiaomi you need to allow manually the option to get when app is closed.

@adirzoari, I use Tecno Camon X, Android 8.1, how do you manually set that?

@Mixpeal did you get any solution for this?
@adirzoari so this will vary from phone to phone?

When i see logs on adb logcat i can see notifcation is being recieved when i send notification to my phone but for some reason it is not being shown.

I did all the 3 steps above.

this is my logs
10-18 21:04:22.571 1433 1466 W BroadcastQueue: don't launch com.package_name/10250 for broadcast Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 mCallingUid=10012 pkg=com.package_name (has extras) }
10-18 21:04:22.572 3869 3869 W GCM : broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.package_name (has extras) }

UPDATE: I think this was a problem of phone.
refer this link=> https://stackoverflow.com/questions/44847835/android-fcm-not-receiving-notifications-when-app-is-removed-from-background/45810771#45810771

Was this page helpful?
0 / 5 - 0 ratings