2

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.

Floydus
  • 81
  • 1
  • 6
  • Can you show where did you put native java class in ionic project? – Mirza Brunjadze Nov 15 '17 at 11:42
  • For the Android portion it is under platform->android->src->com. From there you should see a folder with your project name and a sub folder for your project id. In short exactly where your MainActivity.java is located. Don't forget to add the service portion inside the AndroidManifest.xml. – Floydus Nov 21 '17 at 15:04

1 Answers1

0

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.

Floydus
  • 81
  • 1
  • 6