This is my backend code
$fields = array(
'app_id' => "XXX",
'include_player_ids' => $player_id_arr,
'data' => array("type" => $type, "data" => $data),
'content_available' => true,
'ios_sound' => $notification,
'priority' => 10,
);
if (isset($message)) {
$fields['contents'] = array("en" => $message);
}
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
...
Everything is simple: when $message
variable is NOT empty it send usual push notification and when $message
is empty, it send background/data notification.
On React Native side I use
OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent => {
console.log("notification: ", notificationReceivedEvent);
notificationReceivedEvent.complete(notification);
});
to receive notification. This works fine for iOS(usual push notification and background/data notification) and for Android(usual push notification) but it doesn't work for Android(background/data notification).
According to the https://documentation.onesignal.com/docs/service-extensions#android-notification-extender-service I have created android/app/src/main/java/com/example/NotificationServiceExtension.java
file with the following content:
package com.example;
import android.content.Context;
import android.util.Log;
import org.json.JSONObject;
import com.onesignal.OSNotification;
import com.onesignal.OSMutableNotification;
import com.onesignal.OSNotificationReceivedEvent;
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.graphics.Color;
import java.math.BigInteger;
@SuppressWarnings("unused")
public class NotificationServiceExtension implements OSRemoteNotificationReceivedHandler {
@Override
public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
OSNotification notification = notificationReceivedEvent.getNotification();
// Example of modifying the notification's accent color
OSMutableNotification mutableNotification = notification.mutableCopy();
mutableNotification.setExtender(builder -> {
// Sets the accent color to Green on Android 5+ devices.
// Accent color controls icon and action buttons on Android 5+. Accent color does not change app title on Android 10+
builder.setColor(new BigInteger("FF00FF00", 16).intValue());
// Sets the notification Title to Red
Spannable spannableTitle = new SpannableString(notification.getTitle());
spannableTitle.setSpan(new ForegroundColorSpan(Color.RED),0,notification.getTitle().length(),0);
builder.setContentTitle(spannableTitle);
// Sets the notification Body to Blue
Spannable spannableBody = new SpannableString(notification.getBody());
spannableBody.setSpan(new ForegroundColorSpan(Color.BLUE),0,notification.getBody().length(),0);
builder.setContentText(spannableBody);
//Force remove push from Notification Center after 30 seconds
builder.setTimeoutAfter(30000);
return builder;
});
JSONObject data = notification.getAdditionalData();
Log.i("OneSignalExample", "Received Notification Data: " + data);
// If complete isn't call within a time period of 25 seconds, OneSignal internal logic will show the original notification
// To omit displaying a notification, pass `null` to complete()
notificationReceivedEvent.complete(mutableNotification);
}
}
and add <meta-data android:name="com.onesignal.NotificationServiceExtension" android:value="com.example.NotificationServiceExtension" />
to the android/app/src/main/AndroidManifest.xml
file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.android.vending.BILLING"/>
<application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true">
<meta-data android:name="com.onesignal.NotificationServiceExtension" android:value="com.example.NotificationServiceExtension" />
...
But I still can't receive background/data notification on Android.