Reputation: 1824
How to pop out system "request notification access" window on Android with Ionic Capacitor ?
Documentation for PushNotifications.requestPermissions()
says "android always receive notification" which is false. Some brands like Samsung or user could disable them by default. :
**
* Request permission to receive push notifications.
*
* On Android it doesn't prompt for permission because you can always
* receive push notifications.
*
* On iOS, the first time you use the function, it will prompt the user
* for push notification permission and return granted or denied based
* on the user selection. On following calls it will get the current status of
* the permission without prompting again.
*
* @since 1.0.0
*/
requestPermissions(): Promise<PermissionStatus>;
Also added this to AndroidManifest.xml but nothing.
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
Upvotes: -1
Views: 4307
Reputation: 1
Capacitor - 3, Requesting push notification permissions will not work in the traditional way for android API Level 33+. It can be achieved with android layer on top of it. Make the below changes in MainActivity.java file, to prompt user for notification permission.
MainActivity.java File - android\app\src\main\java\project\app\MainActivity.java
package com.app;
import android.Manifest;
import android.os.Bundle;
import android.os.Build;
import android.os.Handler;
import androidx.core.content.ContextCompat;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import com.getcapacitor.BridgeActivity;
public class MainActivity extends BridgeActivity {
public static final int DOWNLOAD_REQUEST_CODE = 1024;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CheckNotifications();
}
public void CheckNotifications() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
PackageManager.PERMISSION_GRANTED) {
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
} else {
requestPermission.launch(Manifest.permission.POST_NOTIFICATIONS);
}
}
}
private ActivityResultLauncher<String> requestPermission =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
} else {
}
});
}
Upvotes: 0
Reputation: 53351
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
Was added on Android 13, you can only request it in Android 13 devices, in previous Android versions it won’t prompt.
Capacitor 4 doesn’t officially support that permission, it request the permission when you create a notification channel, but in some devices it won’t prompt until next app launch.
If you want to request the permission you have to use Capacitor 5, which is alpha at the moment and also version 5 of @capacitor/push-notifications
, which is also alpha at the moment. You can install them using next
npm tag, i.e. npm install @capacitor/push-notifications@next
Upvotes: 3
Reputation: 929
The docs show and example:
const registerNotifications = async () => {
let permStatus = await PushNotifications.checkPermissions();
if (permStatus.receive === 'prompt') {
permStatus = await PushNotifications.requestPermissions();
}
if (permStatus.receive !== 'granted') {
throw new Error('User denied permissions!');
}
await PushNotifications.register();
}
https://capacitorjs.com/docs/apis/push-notifications
Upvotes: -1