Search code examples
javanotificationsionic2onesignalsilent

NotificationExtenderService not firing handleNotificationReceived


I am extending NotificationExtenderService inside a Java class as per OneSignal documentation here One signal doc, but I realized that returning true prevents the notification from showing. Perfect so far, the problem is I still need to trigger the handleNotificationReceived inside my typescript code.

Here below the Java code extending :

public class NotificationExtenderBareBonesExample extends NotificationExtenderService {
@Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
    receivedResult.payload.lockScreenVisibility = Integer.parseInt("-1");
    JSONObject data = receivedResult.payload.additionalData;
    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.
            builder = builder.setVisibility(Integer.parseInt("-1"));
            builder = builder.setVibrate(new long[]{0L});
            return builder.setColor(new BigInteger("800d2f66", 16).intValue());
        }
    };
    OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
    Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);
    return true;
}

}

The issue I am having is that I need to call displayNotification for having my NotificationReceivedHandler triggered (it analyses the additionalData and run portions accordingly).

Ionic 2 code:

this.oneSignal.handleNotificationReceived().subscribe((result) => {
  alert('received ' + JSON.stringify(result));
  thisRef.writeDebug('notification received');
  let data = result.payload.additionalData;
  let chatmessage = "";
  if (data != null) {
      if(typeof data !== 'undefined' && data.length != 0)
      {
        if(typeof data.chatmessage !== 'undefined')
          console.log('chatmessage ok');
        if(typeof data.id !== 'undefined')
          thisRef._callbackOnChatMessage({chatmessage: data.chatmessage, id: data.id.toString()})
        if(typeof data.shortgeolocation !== 'undefined')
        {
          this._callbackRequestShortLocation(data.shortgeolocation);
        }
      }
      if(typeof data.othertyping !== 'undefined' && typeof thisRef._callbackOnOtherTyping !== 'undefined')
      {
        thisRef._callbackOnOtherTyping(data.othertyping);
      }
  }
});

Here is a preview of my notification once it arrives into my Ionic handler:

enter image description here

Is there any way to change the displayType and Shown value to be respectively 0 and false from the NotificationExtenderService so that my NotificationReceivedHandler in Ionic is called and the alert not shown? (I got this behaviour working under iOS, struggling to do the same in Android).

SOLVED, partially, I had to downgrade onesignal library to 3.5.6 in platforms/android.properties, using cordova.system.library.[keep your number x]=com.onesignal:OneSignal:3.5.6 fixes the issue. For ionic, the file is under platforms/android/ project.properties in Android platform

See this post absolutely related.


Solution

  • I got a mail from OneSignal stating that version 3.6.2 was fixing this issue. So Android property should be com.onesignal:OneSignal:3.6.2 now. See my updated question for updating the file.