Skip to content

[messaging] requestPermissions on Android 13 #159

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

Closed
fpaaske opened this issue Nov 22, 2022 · 4 comments
Closed

[messaging] requestPermissions on Android 13 #159

fpaaske opened this issue Nov 22, 2022 · 4 comments

Comments

@fpaaske
Copy link

fpaaske commented Nov 22, 2022

I'm running into issues with requestPermission on Android 13.

in app.gradle

targetSdkVersion 33

in manifest

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

in package.json

deps
    "@nativescript/angular": "~14.2.0",
    "@nativescript/core": "~8.3.5",
    "@nativescript/firebase-core": "^2.4.4",
    "@nativescript/firebase-crashlytics": "^2.4.4",
    "@nativescript/firebase-messaging": "^2.4.4",

dev-deps
    "@nativescript/android": "8.3.1",
    "@nativescript/ios": "8.3.3",
    "@nativescript/types": "~8.3.1",

I have this code:

main.ts

import {firebase} from '@nativescript/firebase-core';
import '@nativescript/firebase-messaging';
import '@nativescript/firebase-crashlytics';

some.service.ts

constructor()
    firebase().initializeApp()
      .then(firebaseApp => {
        const messaging = firebase().messaging();
        messaging.onMessage(this.onMessage);
        messaging.onNotificationTap(this.onNotificationTap);
      })
      .catch(error => this.loggerService.error('Firebase::Failed to initialize', error));

In another method:

registerToken()
    const messaging = firebase().messaging();
    messaging.requestPermission({ios: iosPermissions}).then(authStatus => {
      const granted = authStatus === AuthorizationStatus.AUTHORIZED || authStatus === AuthorizationStatus.PROVISIONAL;
      if (granted) {
        messaging.registerDeviceForRemoteMessages()
          .then(result => {
            messaging.getToken().then(token => this.storeToken(token));
            messaging.onToken(this.onTokenCallback);
          })
          .catch(error => this.loggerService.error('register failed', error));
      }
    }).catch(error => this.loggerService.error('request permission failed', error));

Then I have "some.component.ts" using the "some.service.ts", and I get this error

request permission failed Error: Cannot assign primitive value to array of objects.
  at requestPermission (file: src/webpack:/my-app/node_modules/@nativescript/firebase-messaging-core/index.android.js:258:0)
  ...

It points to this code (index.android.js:258:0):

const perms = Array.create('java.lang.String', 1);
perms[0] = android.Manifest.permission.POST_NOTIFICATIONS; // line 258
activity.requestPermissions(perms, 1001);

This is working fine in earlier Android versions. (also without the app.gradle and manifest changes).
Any tips on how to solve this?

I checked two other cases #125 and #144, but they seemed to have other issues.

@kefahB
Copy link
Contributor

kefahB commented Nov 24, 2022

HI,

Did you tried to delayed the permission request with settimeout() maybe you request permission too early !?

@fpaaske
Copy link
Author

fpaaske commented Nov 24, 2022

Thanks for the tip, @kefahB, but even adding a 5 second delay did not make any difference.

It seems like the issue is the type of android.Manifest.permission.POST_NOTIFICATIONS. Maybe some of my dependencies or configurations are incorrect?

@kefahB
Copy link
Contributor

kefahB commented Nov 24, 2022

Did you installed SDK 33 ? if not begin with this first and then if it doesn't work try to set compileSdkVersion 33 at app.gradle

@fpaaske
Copy link
Author

fpaaske commented Nov 24, 2022

I had already installed SDK 33, but compileSdk 33 in app.gradle was the key. It works now, thanks!

@fpaaske fpaaske closed this as completed Nov 24, 2022
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