OneSignal / OneSignal-Cordova-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your Ionic, PhoneGap CLI, PhoneGap Build, Cordova, or Sencha Touch app with OneSignal. Supports Android, iOS, and Amazon's Fire OS platforms. https://onesignal.com
Other
249 stars 197 forks source link

[question]: NotificationServiceExtension: How to notify Ionic application when recieve background/data/silent notification? #999

Open anatoly-spb opened 1 month ago

anatoly-spb commented 1 month ago

How can we help?

I want to receive background/data/silent notification in my Ionic Application based on onesignal-cordova-plugin@5.1.4 and Capacitor:

import { Component } from '@angular/core';
import { IonApp, IonRouterOutlet } from '@ionic/angular/standalone';
import OneSignal from 'onesignal-cordova-plugin';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  standalone: true,
  imports: [IonApp, IonRouterOutlet],
})
export class AppComponent {
  constructor() {
    OneSignal.Debug.setLogLevel(6)
    OneSignal.initialize("xxxx")
    OneSignal.Notifications.addEventListener('click', async (e) => {
      let clickData = await e.notification;
      console.log("Notification Clicked : "  + JSON.stringify(clickData));
    })
    OneSignal.Notifications.addEventListener('foregroundWillDisplay', async (e) => {
      let notification = await e.getNotification();
      console.log("Notification Foreground Will Displayed : "  + JSON.stringify(notification));
    })
    OneSignal.Notifications.addEventListener('permissionChange', async (e) => {
      let success = await e;
      console.log("Notification Permission Changed : "  + success);
    })
    OneSignal.Notifications.requestPermission(true).then((success: Boolean) => {
      console.log("Notification permission granted " + success);
    })
    OneSignal.User.pushSubscription.getIdAsync().then(id=>{
      console.log("Notification Subscription ID: " + id);
    })
    // XXXXXXXXXX
  }
}

. According to documentation https://documentation.onesignal.com/docs/data-notifications I have implemented NotificationServiceExtension for Android:

import androidx.annotation.Keep;
import androidx.annotation.NonNull;

import com.onesignal.debug.internal.logging.Logging;
import com.onesignal.notifications.IDisplayableMutableNotification;
import com.onesignal.notifications.INotificationReceivedEvent;
import com.onesignal.notifications.INotificationServiceExtension;
@Keep
public class NotificationServiceExtension implements INotificationServiceExtension {
  @Override
  public void onNotificationReceived(@NonNull INotificationReceivedEvent event) {
    IDisplayableMutableNotification notification = event.getNotification();

    Logging.info(String.format("NotificationServiceExtension::onNotificationReceived: notificationId=%s, rawPayload=%s", notification.getNotificationId(), notification.getRawPayload()), null);
    // YYYYYYYYYYY
  }
}

But what should I call inside onNotificationReceived instead of YYYYYYYYYYY to pass this notification to Ionic Application based on Capacitor and what should I write instead of XXXXXXXXXX to subscribe on background/data/silent notification?

Thanks in advance, Anatoly Shirokov

Code of Conduct

jkasten2 commented 1 week ago

@anatoly-spb

Keep in mind that background/silent are not always the best fit, so it is recommend you provide more details on your goal to see if there are better options.

We don't have a specific guide on doing bridging like this but you could use some like Ionic App Events if you need get some information from Java / Kotlin native to JavaScript. https://capacitorjs.com/docs/v2/plugins/android#app-events