Skip to content

Commit 4dac0d7

Browse files
authored
feat(fcm): add missing sendNotification() method (#378)
1 parent 86d35de commit 4dac0d7

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

messaging/app/src/main/java/com/google/firebase/example/messaging/MyFirebaseMessagingService.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package com.google.firebase.example.messaging;
22

3+
import android.app.NotificationChannel;
4+
import android.app.NotificationManager;
5+
import android.app.PendingIntent;
36
import android.content.Context;
7+
import android.content.Intent;
8+
import android.media.RingtoneManager;
9+
import android.net.Uri;
10+
import android.os.Build;
411
import android.util.Log;
512

613
import androidx.annotation.NonNull;
14+
import androidx.core.app.NotificationCompat;
715
import androidx.work.OneTimeWorkRequest;
816
import androidx.work.WorkManager;
917
import androidx.work.Worker;
@@ -84,6 +92,36 @@ private void sendRegistrationToServer(String token) {
8492
// TODO: Implement this method to send token to your app server.
8593
}
8694

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+
87125
public static class MyWorker extends Worker {
88126

89127
public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {

messaging/app/src/main/java/com/google/firebase/example/messaging/kotlin/MyFirebaseMessagingService.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package com.google.firebase.example.messaging.kotlin
22

3+
import android.app.NotificationChannel
4+
import android.app.NotificationManager
5+
import android.app.PendingIntent
36
import android.content.Context
7+
import android.content.Intent
8+
import android.media.RingtoneManager
9+
import android.os.Build
410
import android.util.Log
11+
import androidx.core.app.NotificationCompat
512
import androidx.work.OneTimeWorkRequest
613
import androidx.work.WorkManager
714
import androidx.work.Worker
815
import androidx.work.WorkerParameters
16+
import com.google.firebase.example.messaging.R
917
import com.google.firebase.messaging.FirebaseMessagingService
1018
import com.google.firebase.messaging.RemoteMessage
1119

@@ -75,6 +83,34 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
7583
Log.d(TAG, "sendRegistrationTokenToServer($token)")
7684
}
7785

86+
private fun sendNotification(messageBody: String) {
87+
val intent = Intent(this, MainActivity::class.java)
88+
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
89+
val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
90+
PendingIntent.FLAG_IMMUTABLE)
91+
92+
val channelId = "fcm_default_channel"
93+
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
94+
val notificationBuilder = NotificationCompat.Builder(this, channelId)
95+
.setContentTitle("FCM Message")
96+
.setContentText(messageBody)
97+
.setAutoCancel(true)
98+
.setSound(defaultSoundUri)
99+
.setContentIntent(pendingIntent)
100+
101+
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
102+
103+
// Since android Oreo notification channel is needed.
104+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
105+
val channel = NotificationChannel(channelId,
106+
"Channel human readable title",
107+
NotificationManager.IMPORTANCE_DEFAULT)
108+
notificationManager.createNotificationChannel(channel)
109+
}
110+
111+
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
112+
}
113+
78114
companion object {
79115
private const val TAG = "MyFirebaseMsgService"
80116
}

0 commit comments

Comments
 (0)