|
1 | 1 | package com.google.firebase.example.messaging;
|
2 | 2 |
|
| 3 | +import android.Manifest; |
3 | 4 | import android.annotation.SuppressLint;
|
| 5 | +import android.content.pm.PackageManager; |
| 6 | +import android.os.Build; |
4 | 7 | import android.os.Bundle;
|
5 | 8 |
|
6 | 9 | import androidx.annotation.NonNull;
|
| 10 | +import androidx.annotation.RequiresApi; |
7 | 11 | import androidx.appcompat.app.AppCompatActivity;
|
| 12 | +import androidx.core.content.ContextCompat; |
| 13 | + |
8 | 14 | import android.util.Log;
|
9 | 15 | import android.widget.Toast;
|
10 | 16 |
|
|
19 | 25 | public class MainActivity extends AppCompatActivity {
|
20 | 26 |
|
21 | 27 | private static final String TAG = "MainActivity";
|
| 28 | + private static final int NOTIFICATION_REQUEST_CODE = 1234; |
22 | 29 |
|
23 | 30 | @Override
|
24 | 31 | protected void onCreate(Bundle savedInstanceState) {
|
@@ -102,4 +109,41 @@ public void onComplete(@NonNull Task<String> task) {
|
102 | 109 | });
|
103 | 110 | // [END log_reg_token]
|
104 | 111 | }
|
| 112 | + |
| 113 | + @RequiresApi(33) |
| 114 | + // [START ask_post_notifications] |
| 115 | + private void askNotificationPermission() { |
| 116 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == |
| 117 | + PackageManager.PERMISSION_GRANTED) { |
| 118 | + // FCM SDK (and your app) can post notifications. |
| 119 | + } else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { |
| 120 | + // TODO: display an educational UI explaining to the user the features that will be enabled |
| 121 | + // by them granting the POST_NOTIFICATION permission. This UI should provide the user |
| 122 | + // "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission. |
| 123 | + // If the user selects "No thanks," allow the user to continue without notifications. |
| 124 | + } else { |
| 125 | + // Directly ask for the permission |
| 126 | + requestPermissions(new String[] { Manifest.permission.POST_NOTIFICATIONS }, NOTIFICATION_REQUEST_CODE); |
| 127 | + } |
| 128 | + } |
| 129 | + // [END ask_post_notifications] |
| 130 | + |
| 131 | + // [START handle_ask_post_notifications_request] |
| 132 | + @Override |
| 133 | + public void onRequestPermissionsResult(int requestCode, String[] permissions, |
| 134 | + int[] grantResults) { |
| 135 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 136 | + switch (requestCode) { |
| 137 | + case NOTIFICATION_REQUEST_CODE: |
| 138 | + // If request is cancelled, the result arrays are empty. |
| 139 | + if (grantResults.length > 0 && |
| 140 | + grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 141 | + // FCM SDK (and your app) can post notifications. |
| 142 | + } else { |
| 143 | + // TODO: Inform user that that your app will not show notifications. |
| 144 | + } |
| 145 | + return; |
| 146 | + } |
| 147 | + } |
| 148 | + // [END handle_ask_post_notifications_request] |
105 | 149 | }
|
0 commit comments