Skip to content

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

@anatoly-spb

Description

@anatoly-spb

How can we help?

I want to receive background/data/silent notification in my Ionic Application based on [email protected] 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

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions