|
1 | 1 | package com.google.firebase.example.messaging;
|
2 | 2 |
|
| 3 | +import android.app.NotificationChannel; |
| 4 | +import android.app.NotificationManager; |
| 5 | +import android.app.PendingIntent; |
3 | 6 | import android.content.Context;
|
| 7 | +import android.content.Intent; |
| 8 | +import android.media.RingtoneManager; |
| 9 | +import android.net.Uri; |
| 10 | +import android.os.Build; |
4 | 11 | import android.util.Log;
|
5 | 12 |
|
6 | 13 | import androidx.annotation.NonNull;
|
| 14 | +import androidx.core.app.NotificationCompat; |
7 | 15 | import androidx.work.OneTimeWorkRequest;
|
8 | 16 | import androidx.work.WorkManager;
|
9 | 17 | import androidx.work.Worker;
|
@@ -84,6 +92,36 @@ private void sendRegistrationToServer(String token) {
|
84 | 92 | // TODO: Implement this method to send token to your app server.
|
85 | 93 | }
|
86 | 94 |
|
| 95 | + private void sendNotification(String messageBody) { |
| 96 | + Intent intent = new Intent(this, MainActivity.class); |
| 97 | + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 98 | + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, |
| 99 | + PendingIntent.FLAG_IMMUTABLE); |
| 100 | + |
| 101 | + String channelId = "fcm_default_channel"; |
| 102 | + Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |
| 103 | + NotificationCompat.Builder notificationBuilder = |
| 104 | + new NotificationCompat.Builder(this, channelId) |
| 105 | + .setContentTitle("FCM Message") |
| 106 | + .setContentText(messageBody) |
| 107 | + .setAutoCancel(true) |
| 108 | + .setSound(defaultSoundUri) |
| 109 | + .setContentIntent(pendingIntent); |
| 110 | + |
| 111 | + NotificationManager notificationManager = |
| 112 | + (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
| 113 | + |
| 114 | + // Since android Oreo notification channel is needed. |
| 115 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 116 | + NotificationChannel channel = new NotificationChannel(channelId, |
| 117 | + "Channel human readable title", |
| 118 | + NotificationManager.IMPORTANCE_DEFAULT); |
| 119 | + notificationManager.createNotificationChannel(channel); |
| 120 | + } |
| 121 | + |
| 122 | + notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); |
| 123 | + } |
| 124 | + |
87 | 125 | public static class MyWorker extends Worker {
|
88 | 126 |
|
89 | 127 | public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
|
|
0 commit comments