16

I'm working an application and I need to use push notification. I know that push notification are a normal permission so I can't ask it at run time. But I would insert in the permission when the user downloads and installs the application, and the notice that the application should send a push notification.

How can I do it? Do I have to insert something in the manifest?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Paul
  • 271
  • 1
  • 3
  • 7
  • Possible duplicate : https://stackoverflow.com/questions/37294076/push-notifications-gcm-permission-at-runtime – Haresh Chhelana Jun 01 '17 at 10:37
  • You can replicate runtime behaviour by showing your own dialog and maintaining a `SharedPreference` to see if it granted or not.. – Veneet Reddy Jun 01 '17 at 10:40
  • Pushes and notifications 2 entirely different things on Android. It's unclear what you're asking about. – Agent_L Apr 05 '18 at 08:01

4 Answers4

52

As answered here, you don't need permissions for push notifications.

Actually the push notification permission lie in the normal category permission like Internet permission, not in dangerous category permission.

You don't have to ask for push notification permissions.

While Contacts/Locations are the dangerous permissions because you are accessing user data. So it is always needed to ask the user to allow it.

https://developer.android.com/guide/topics/security/permissions.html

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Kunal Chawla
  • 1,046
  • 2
  • 10
  • 21
3

But I would insert in the permission when the user downloads and installs the application, and the notice that the application should send a push notification. How can I do it?

That is not supported, sorry.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
CommonsWare
  • 954,112
  • 185
  • 2,315
  • 2,367
1

On and after Android 13 (Tramisu), you need to get this notification permission by using : <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

For more details: https://developer.android.com/about/versions/13/changes/notification-permission#user-choice

Ercan Akkök
  • 111
  • 1
  • 4
-2

Try this:

In your Activity class, add the below code:

private static final int NOTIFICATION_PERMISSION_CODE = 123;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);

    requestNotificationPermission();

    // Some code
}

 private void requestNotificationPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NOTIFICATION_POLICY) == PackageManager.PERMISSION_GRANTED)
        return;

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_NOTIFICATION_POLICY)) {

    }

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY}, NOTIFICATION_PERMISSION_CODE );
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    // Checking the request code of our request
    if (requestCode == NOTIFICATION_PERMISSION_CODE ) {

        // If permission is granted
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Displaying a toast
            Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
        } else {
            // Displaying another toast if permission is not granted
            Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
        }
    }
}
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Fakhriddin Abdullaev
  • 3,234
  • 2
  • 27
  • 31