问题
I'm writing an application in Ionic 3 that uses the Cordova OneSignal plugin (onesignal-cordova-plugin 2.8.4).
Scenario: The application is killed and a push notification is received: I need to know that the notification was received BEFORE the notification is clicked or the application is opened.
The plan is to write code that runs as soon as a notification is received that will send a "receipt" to the main server. This is only happening if the Ionic application is running in the background or opened. If the Ionic application is "killed" code doesn't run.
I have followed the steps on this post to extend the NotificationExtenderService. I was able to add the Java file that I created to the AndroidManifest.xml file and the compiler seems to find it. I get the following error :
[cordova] protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
[cordova] ^
[cordova] symbol: class OSNotificationReceivedResult
[cordova] location: class NotificationExtenderExample
My NotificationExtender.java file (this file is at platforms\android\app\src\main\java\com\plugin\gcm\NotificationExtenderExample.java)
import android.support.v4.app.NotificationCompat;
import com.onesignal.OSNotificationPayload;
import com.onesignal.NotificationExtenderService;
import java.math.BigInteger;
public class NotificationExtenderExample extends NotificationExtenderService {
@Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
OverrideSettings overrideSettings = new OverrideSettings();
overrideSettings.extender = new NotificationCompat.Extender() {
@Override
public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
// Sets the background notification color to Green on Android 5.0+ devices.
return builder.setColor(new BigInteger("FF00FF00", 16).intValue());
}
};
OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);
return true;
}
}
I added the following code to my AndroidManifest.xml (this file is at platforms\android\app\src\main\AndroidManifest.xml)
<service android:exported="false" android:name=".NotificationExtenderExample" android:permission="android.permission.BIND_JOB_SERVICE">
<intent-filter>
<action android:name="com.onesignal.NotificationExtender" />
</intent-filter>
</service>
来源:https://stackoverflow.com/questions/60460880/ionic-2-cordova-onesignal-notificationextenderservice