简体   繁体   中英

Onesignal Notification permission issue in android 13, No permission dialog box appears

No permission dialog box appears.Only able to fetch getIds ,Cannot able fetch **pushToken ** in android 13 device.By this notification is not able to fire.

Facing onesignal notification issue in android 13 devices. Notification is not fired in device because. I checked the log of fetching ids while **initialzing ** app in app.component.ts

it is something like this.

log of android 13 device (Notification completely not working ): Here the permission is not enabled in app info.

getIds: {"userId":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","pushToken":""}

log of android 12 or lower device (Notification completely working ): Here the permission is enabled in app info.

getIds: {"userId":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","pushToken":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}

====package.json====

{
  "name": "MyApp",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular-devkit/schematics": "^14.0.6",
    "@angular/common": "^14.0.0",
    "@angular/core": "^14.0.0",
    "@angular/forms": "^14.0.0",
    "@angular/platform-browser": "^14.0.0",
    "@angular/platform-browser-dynamic": "^14.0.0",
    "@angular/router": "^14.0.0",
    "@awesome-cordova-plugins/android-permissions": "^5.45.0",
    "@awesome-cordova-plugins/app-version": "^6.2.0",
    "@awesome-cordova-plugins/camera": "^6.2.0",
    "@awesome-cordova-plugins/core": "^6.2.0",
    "@awesome-cordova-plugins/geolocation": "^5.44.0",
    "@awesome-cordova-plugins/google-analytics": "^6.2.0",
    "@awesome-cordova-plugins/in-app-browser": "^6.2.0",
    "@awesome-cordova-plugins/keyboard": "^6.2.0",
    "@awesome-cordova-plugins/location-accuracy": "^5.45.0",
    "@awesome-cordova-plugins/native-geocoder": "^6.2.0",
    "@awesome-cordova-plugins/network": "^6.2.0",
    "@awesome-cordova-plugins/social-sharing": "^6.2.0",
    "@awesome-cordova-plugins/splash-screen": "^6.2.0",
    "@awesome-cordova-plugins/status-bar": "^6.2.0",
    "@awesome-cordova-plugins/system-alert-window-permission": "^6.2.0",
    "@capacitor/app": "4.1.0",
    "@capacitor/core": "4.4.0",
    "@capacitor/haptics": "4.0.1",
    "@capacitor/keyboard": "4.0.1",
    "@capacitor/status-bar": "4.0.1",
    "@ionic-native/core": "^5.36.0",
    "@ionic-native/onesignal": "^5.27.0",
    "@ionic/angular": "^6.1.14",
    "@ionic/cordova-builders": "^7.0.0",
    "@ionic/storage": "^2.2.0",
    "angular-cropperjs": "^1.0.2",
    "cropperjs": "^1.5.13",
    "ionicons": "^6.0.3",
    "lodash": "^4.17.21",
    "moment": "^2.29.4",
    "rxjs": "~6.6.0",
    "sentry-cordova": "^1.0.4",
    "tslib": "^2.2.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.0.6",
    "@angular-eslint/builder": "~13.0.1",
    "@angular-eslint/eslint-plugin": "~13.0.1",
    "@angular-eslint/eslint-plugin-template": "~13.0.1",
    "@angular-eslint/template-parser": "~13.0.1",
    "@angular/cli": "^14.0.6",
    "@angular/compiler": "^14.0.0",
    "@angular/compiler-cli": "^14.0.0",
    "@angular/language-service": "^14.0.0",
    "@capacitor/cli": "4.4.0",
    "@ionic/angular-toolkit": "^6.0.0",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "@typescript-eslint/eslint-plugin": "5.3.0",
    "@typescript-eslint/parser": "5.3.0",
    "com.razorpay.cordova": "^1.4.15",
    "cordova-android": "^10.1.1",
    "cordova-plugin-add-swift-support": "^2.0.2",
    "cordova-plugin-android-permissions": "^1.1.4",
    "cordova-plugin-app-version": "^0.1.14",
    "cordova-plugin-camera": "^6.0.0",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-geolocation": "^4.1.0",
    "cordova-plugin-google-analytics": "^1.9.2",
    "cordova-plugin-inappbrowser": "^5.0.0",
    "cordova-plugin-ionic-keyboard": "^2.2.0",
    "cordova-plugin-ionic-webview": "^5.0.0",
    "cordova-plugin-nativegeocoder": "^3.4.1",
    "cordova-plugin-network-information": "^3.0.0",
    "cordova-plugin-request-location-accuracy": "^2.3.0",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-system-alert-window-permission": "0.0.6",
    "cordova-plugin-x-socialsharing": "^6.0.4",
    "es6-promise-plugin": "^4.2.2",
    "eslint": "^7.6.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jsdoc": "30.7.6",
    "eslint-plugin-prefer-arrow": "1.2.2",
    "jasmine-core": "~3.8.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~6.3.2",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "onesignal-cordova-plugin": "^2.11.4",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "typescript": "~4.7.3"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-inappbrowser": {},
      "cordova-plugin-camera": {},
      "cordova-plugin-android-permissions": {},
      "cordova-plugin-request-location-accuracy": {},
      "cordova-plugin-geolocation": {},
      "cordova-plugin-nativegeocoder": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-device": {},
      "cordova-plugin-ionic-webview": {},
      "cordova-plugin-ionic-keyboard": {},
      "com.razorpay.cordova": {},
      "cordova-plugin-x-socialsharing": {},
      "cordova-plugin-network-information": {},
      "cordova-plugin-google-analytics": {
        "GMS_VERSION": "18.0.2"
      },
      "cordova-plugin-app-version": {},
      "sentry-cordova": {
        "SENTRY_ANDROID_SDK_VERSION": "6.4.1"
      },
      "onesignal-cordova-plugin": {},
      "cordova-plugin-system-alert-window-permission": {}
    },
    "platforms": [
      "android"
    ]
  }
}

====app.component.ts====

constructor(
    private menu: MenuController,
    private navCtrl: NavController,
    private platform: Platform,
    private authService: AuthenticationService,
    private httpSignin: SigninService,
    private alertConfirm: ToastService,
    public globalVar: GlobalvariableService,
    public msg: MessageParserService,
    private storage: AppStorageService,
    public globalFun: GlobalFunctionService,
    public oneSignal: OnesignalService,
    private appVersion: AppVersion,
    private googleAnalytics: GoogleAnalytics,
    private iab: InAppBrowser,
    private statusBar: StatusBar
  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(async () => {
      this.oneSignal.OneSignalNotification()
    });
  }

=====onesignal.service.ts====

import { Injectable } from '@angular/core';
import { NavigationExtras, Router } from '@angular/router';
import { AlertController, LoadingController, NavController, Platform, PopoverController } from '@ionic/angular';
import { AppStorageService } from './app-storage.service';
import { GlobalvariableService } from './globalvariable.service';
import { OneSignal } from '@ionic-native/onesignal/ngx';

@Injectable({
  providedIn: 'root'
})
export class OnesignalService {

  constructor(
    private oneSignal: OneSignal,
    private platform: Platform,
    private globalVar: GlobalvariableService,
    private loadingController: LoadingController,
    private storage: AppStorageService,
    private navCtrl: NavController,
    private popoverCtrl: PopoverController,
    public alrtCrtl: AlertController,
    private router: Router
  ) {
  }

  async OneSignalNotification() {
    await this.platform.ready().then(async () => {
      if (this.platform.is('cordova')) {
        // Initialise plugin with OneSignal service
        //  await this.oneSignal.startInit(this.storage.OneSignalAppID, this.storage.SenderID);
        await this.oneSignal.startInit(this.globalVar.oneSignalAppId, this.globalVar.oneSignalSenderId);  
        // When a push notification is received handle
        // how the application will respond
        await this.oneSignal.handleNotificationReceived().subscribe((msg) => {
          // Log data received from the push notification service
          console.log('Notification received');
          console.dir(msg);
        });
        this.OneSignalPlayerID();
        // When a push notification is opened by the user
        // handle how the application will respond
        await this.oneSignal.handleNotificationOpened().subscribe((msg) => {
          // Log data received from the push notification service
          console.log('Notification opened');
          let notification_obj = msg.notification.payload.additionalData;
          if (notification_obj) {
            if (this.globalVar.loginInfo != '') {
              this.fnOpenNotification(notification_obj);
            } else {
              this.navCtrl.navigateRoot('login');
            }
          }
          // console.dir(msg);
        });
        this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.Notification);
        // End plugin initialisation
        this.oneSignal.endInit();
      } else {
        this.globalVar.playerId = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX';
      }
    });
  }

  async OneSignalPlayerID() {
    await this.platform.ready().then(() => {
      if (this.platform.is('cordova')) {
        this.fnShowLoading();
        // Retrieve the OneSignal user id and the device token
        this.oneSignal.getIds().then((ids) => {
          console.log('getIds: ' + JSON.stringify(ids)); //UNABLE TO FETCH pushToken in android-13 
          // alert('getIds: ' + JSON.stringify(ids));
          // this.storage.deviceId = ids.userId;

          this.globalVar.playerId = ids.userId;
          this.storage.set('player_id', this.globalVar.playerId);
          this.fnhideLoading();
          return ids.userId;
        });
      } else {
        // this.fnhideLoading();
        this.globalVar.playerId = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX';
        this.storage.set('player_id', this.globalVar.playerId);
      }
    });
    // return this.globalVar.playerId;

  }

  async fnShowLoading() {
    await this.loadingController.create({
      message: 'Please wait fetching configuration..',
      spinner: 'circles',
    }).then((ldr: any) => {
      ldr.present()
    });
  }

  async fnhideLoading() {
    setTimeout(async () => {
      let topLoader = await this.loadingController.getTop();
      while (topLoader) {
        if (!(await topLoader.dismiss())) {
          await topLoader.dismiss();
          break;
        }
        topLoader = await this.loadingController.getTop();
      }
    }, 1000);
  }

}

Please help, Thanks in advance.

You already did the Add Notification Permission Prompt step?

If not, you can show prompt with an in app message , or use the not recommended method promptForPushNotificationsWithUserResponse() .

Dont' forget to update your dependencies, you are using older versions.

https://www.npmjs.com/package/onesignal-cordova-plugin

Setup:

import OneSignal from 'onesignal-cordova-plugin';

// Call this function when your app starts
function OneSignalInit(): void {
  // Uncomment to set OneSignal device logging to VERBOSE  
  // OneSignal.setLogLevel(6, 0);

  // NOTE: Update the setAppId value below with your OneSignal AppId.
  OneSignal.setAppId("YOUR_ONESIGNAL_APP_ID");
  OneSignal.setNotificationOpenedHandler(function(jsonData) {
      console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData));
  });

  // Prompts the user for notification permissions.
  //    * Since this shows a generic native prompt, we recommend instead using an In-App Message to prompt for notification permission  to better communicate to your users what notifications they will get.
  OneSignal.promptForPushNotificationsWithUserResponse(function(accepted) {
      console.log("User accepted notifications: " + accepted);
  });
}

Setup documentation: https://documentation.onesignal.com/docs/ionic-sdk-setup

https://documentation.onesignal.com/docs/cordova-sdk-setup#step-4-update-project-settings-to-target-android-13-android-only

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM