Здравствуйте, у меня есть два класса, оба расширяют FirebaseMessagingService
1.Первый класс (FirebaseMessaging. java) для получения уведомлений чата
2. Второй класс (FirebaseMessagingFriendReq. java) для получения уведомлений о запросе на добавление в друзья
Проблема заключается в том, что только одна служба уведомлений работает, если обе службы указаны в файле манифеста. Например,
1.Если обе службы присутствуют в манифесте, только FirebaseMessagingFriendReq . java служба работает
2.Если указана только служба FirebaseMessagingFriendReq. java, то она работает.
3 Если указана только служба FirebaseMessaging. java, то она работает
Но когда оба указаны, только служба FirebaseMessagingFriendReq. java работает. Я хочу, чтобы обе службы работали. Возможно ли это?
FirebaseMessagingFriendReq. java file
public class FirebaseMessagingFriendReq extends FirebaseMessagingService {
private static final String id = "admin_channel";
private DatabaseReference dr;
private FirebaseAuth auth;
private FirebaseUser user;
private String uid;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String notificationTtile = remoteMessage.getNotification().getTitle();
String notificationBody = remoteMessage.getNotification().getBody();
String clickAction = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");
NotificationHelper helper = new NotificationHelper(this);
NotificationCompat.Builder builder=helper.getEDMTChannelNotification(notificationTtile,notificationBody);
int notificationId = (int) System.currentTimeMillis();
Intent intent = new Intent(clickAction);
intent.putExtra("user_id", from_user_id);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
helper.getManager().notify(notificationId,builder.build());
}
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
user = FirebaseAuth.getInstance().getCurrentUser();
uid = user.getUid();
dr = FirebaseDatabase.getInstance().getReference().child("Chat_Profiles").child(uid);
Token t = new Token(FirebaseInstanceId.getInstance().getToken());
dr.child("device_token").setValue(t);
}
}
FirebaseMessaging. java file
public class FirebaseMessaging extends FirebaseMessagingService {
private String channelId = "com.pappu5.navigation";
private static final String id = "admin_channel";
private DatabaseReference dr;
private FirebaseAuth auth;
private FirebaseUser user;
private String uid;
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
SharedPreferences sp = getSharedPreferences("USER", MODE_PRIVATE);
String currentUser = sp.getString("Current_User", "None");
String nType = remoteMessage.getData().get("type");
System.out.println("NTYPE ISSSSSSSSSSsssssssssssssssss "+nType);
if (nType.equals("PostNotification")) {
String sent = remoteMessage.getData().get("sent");
String user = remoteMessage.getData().get("user");
FirebaseUser fu = FirebaseAuth.getInstance().getCurrentUser();
if (fu != null && sent.equals(fu.getUid())) {
if (!currentUser.equals(user)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
showPostNotification(remoteMessage);
} else {
showNormalNotification(remoteMessage);
}
}
}
} else if (nType.equals("ChatNotification") ) {
String sent = remoteMessage.getData().get("sent");
String user = remoteMessage.getData().get("user");
FirebaseUser fu = FirebaseAuth.getInstance().getCurrentUser();
if (fu != null && sent.equals(fu.getUid())) {
if (!currentUser.equals(user)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
sendNotification(remoteMessage);
} else {
sendNormalNotification(remoteMessage);
}
}
}
}
}
private void showNormalNotification(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("id", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(Integer.parseInt(icon))
.setContentText(body)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pi)
.setPriority(Notification.PRIORITY_HIGH);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int j = 0;
if (i > 0) {
j = i;
}
nm.notify(j, builder.build());
}
private void sendNotification(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, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
OreoAndAbove oreo = new OreoAndAbove(this);
Notification.Builder builder = oreo.getNotifications(title, body, pi, sound, icon);
int j = 0;
if (i > 0) {
j = i;
}
oreo.getManager().notify(j, builder.build());
}
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("id", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(Integer.parseInt(icon))
.setContentText(body)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pi);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int j = 0;
if (i > 0) {
j = i;
}
nm.notify(j, builder.build());
}
private void showPostNotification(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("desc");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int i = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
OreoAndAbove oreo = new OreoAndAbove(this);
Notification.Builder builder = oreo.getNotifications(title, body, pi, sound, icon);
int j = 0;
if (i > 0) {
j = i;
}
oreo.getManager().notify(j, builder.build());
}
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
user = FirebaseAuth.getInstance().getCurrentUser();
uid = user.getUid();
dr = FirebaseDatabase.getInstance().getReference().child("Chat_Profiles").child(uid);
Token t = new Token(FirebaseInstanceId.getInstance().getToken());
dr.child("device_token").setValue(t);
}
}
AndroidManifest. xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.pappu5.navigation">
<application
android:name="com.pappu5.navigation.OfflineFeatures"
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SettingsActivity"></activity>
<activity
android:name=".AddPost"
android:parentActivityName=".MainActivity" />
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".NewsDetails" />
<activity android:name=".NavBarActivity" />
<activity android:name=".LoginScreen">
<intent-filter tools:ignore="AppLinkUrlError">
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlreadyActivity" />
<activity
android:name=".ProfileActivity"
android:parentActivityName=".MainActivity" />
<activity
android:name=".ChangeStatus"
android:parentActivityName=".ProfileActivity" />
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat" />
<activity
android:name=".ViewProfile"
android:parentActivityName=".Users">
<intent-filter>
<action android:name="com.pappu5.navigation_TARGET_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Users" />
<activity
android:name=".ChatActivity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="image/*" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
<activity android:name=".SharingActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
<service
android:name=".FirebaseMessagingFriendReq"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter> <!-- This is FriendRequest service-->
</service>
<service
android:name=".notifications.FirebaseMessaging"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service> <!-- This is ChatNotification service-->
<service
android:name=".notifications.FirebaseService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_SERVICE" />
</intent-filter>
</service>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.pappu5.navigation.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths"
/>
</provider>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>