-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirebaseMessaging.java
More file actions
111 lines (90 loc) · 4.41 KB
/
Copy pathFirebaseMessaging.java
File metadata and controls
111 lines (90 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.codewithharry.firebase.notifications;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import com.codewithharry.firebase.ChatActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//get current from shared preferences
SharedPreferences sp = getSharedPreferences("SP_USER", MODE_PRIVATE);
String savedCurrentUser = sp.getString("Current_USERID", "None");
String sent = remoteMessage.getData().get("sent");
String user = remoteMessage.getData().get("user");
FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
if (fUser != null && sent.equals(fUser.getUid())) {
if (!savedCurrentUser.equals(user)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
sendOAndAboveNotification(remoteMessage);
} else {
sendNormalNotification(remoteMessage);
}
}
}
}
private void sendNormalNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int i = Integer.parseInt(user.replaceAll("[//D]", ""));
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(Integer.parseInt(icon))
.setContentText(body)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(defSoundUri)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
int j=0;
if (i>0) {
j=i;
}
notificationManager.notify(j, builder.build());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void sendOAndAboveNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int i = Integer.parseInt(user.replaceAll("[//D]", ""));
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
OreoAndAboveNotification notification1 = new OreoAndAboveNotification(this);
Notification.Builder builder = notification1.getONotification(title, body, pIntent, defSoundUri, icon);
int j=0;
if (i>0) {
j=i;
}
notification1.getManager().notify(j, builder.build());
}
}