|
1 | 1 | package com.google.samples.quickstart.functions.java;
|
2 | 2 |
|
| 3 | +import android.Manifest; |
3 | 4 | import android.app.Notification;
|
4 | 5 | import android.app.NotificationChannel;
|
5 | 6 | import android.app.NotificationManager;
|
| 7 | +import android.content.pm.PackageManager; |
6 | 8 | import android.os.Build;
|
| 9 | + |
7 | 10 | import androidx.core.app.NotificationCompat;
|
8 | 11 | import androidx.core.app.NotificationManagerCompat;
|
| 12 | +import androidx.core.content.ContextCompat; |
| 13 | + |
9 | 14 | import android.util.Log;
|
10 | 15 |
|
11 | 16 | import com.google.firebase.messaging.FirebaseMessagingService;
|
@@ -39,13 +44,17 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
|
39 | 44 | // Check if message contains a data payload.
|
40 | 45 | if (remoteMessage.getData().size() > 0) {
|
41 | 46 | Log.d(TAG, "Message data payload: " + remoteMessage.getData());
|
42 |
| - NotificationManagerCompat manager = NotificationManagerCompat.from(this); |
43 |
| - Notification notification = new NotificationCompat.Builder(this, "Messages") |
44 |
| - .setContentText(remoteMessage.getData().get("text")) |
45 |
| - .setContentTitle("New message") |
46 |
| - .setSmallIcon(R.drawable.ic_stat_notification) |
47 |
| - .build(); |
48 |
| - manager.notify(0, notification); |
| 47 | + // Check if permission to post notifications has been granted |
| 48 | + if (ContextCompat.checkSelfPermission(this, |
| 49 | + Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) { |
| 50 | + NotificationManagerCompat manager = NotificationManagerCompat.from(this); |
| 51 | + Notification notification = new NotificationCompat.Builder(this, "Messages") |
| 52 | + .setContentText(remoteMessage.getData().get("text")) |
| 53 | + .setContentTitle("New message") |
| 54 | + .setSmallIcon(R.drawable.ic_stat_notification) |
| 55 | + .build(); |
| 56 | + manager.notify(0, notification); |
| 57 | + } |
49 | 58 | }
|
50 | 59 | }
|
51 | 60 | }
|
0 commit comments