Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
anatoly-spb opened this issue May 29, 2024 · 1 comment

Comments

@anatoly-spb
Copy link

anatoly-spb commented May 29, 2024

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
@jkasten2
Copy link
Member

jkasten2 commented Jun 21, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants