如何像社交媒体应用程序一样发送和接收通知



我已经成功地使用java和firebase开发了一个聊天应用程序,在与安装了我的应用程序的朋友聊天时,我无法发送和接收聊天通知。我试过用FCM,但没有用。我已经写了代码,当我发送消息时,朋友不会收到应用程序是在前台还是后台的通知,也没有发现错误。请帮帮我。下面是扩展FirebaseMessagingService的类的代码。

@SuppressLint("MissingFirebaseInstanceTokenRefresh")
public class NotifyFirebaseMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull @NotNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

String sent = remoteMessage.getData().get("sent");
String user = remoteMessage.getData().get("user");
SharedPreferences preferences = getSharedPreferences("PREFS", MODE_PRIVATE);
String currentUser = preferences.getString("currentuser", "none");
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if(firebaseUser != null && sent.equals(firebaseUser.getUid())) {
if (!currentUser.equals(user)) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
sendOreoNotification(remoteMessage);
}
else{
sendNotification(remoteMessage);
}
}

}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void sendOreoNotification(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();
assert user != null;
int j = Integer.parseInt(user.replaceAll("[\D]", ""));
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userid", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent,PendingIntent.FLAG_ONE_SHOT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
OreoNotification oreoNotification = new OreoNotification(this);
Notification.Builder builder = oreoNotification.getOreoNotification(title,body,pendingIntent,defaultSound,icon);
int i = 0;
if(j>0){
i = j;
}
oreoNotification.getManager().notify(i, 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();
assert user != null;
int j = Integer.parseInt(user.replaceAll("[\D]", ""));
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userid", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent,PendingIntent.FLAG_ONE_SHOT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(Integer.parseInt(icon))
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int i = 0;
if(j>0){
i = j;
}
notificationManager.notify(i, builder.build());
}
}

我想您可能对sendNotification的功能感到困惑。据我所知,您仅共享的代码接收来自Firebase Cloud Messaging的消息,然后将其显示在本地设备的通知托盘中。

任何代码都不会向Firebase Cloud Messaging发送消息,实际上只有受信任的环境(如您的开发机器、您控制的服务器或云功能/云运行(才支持该消息,也不会从客户端应用程序代码中发送消息。

您还可以在Firebase控制台中发送消息,这非常适合测试收据和有针对性的消息活动。

要了解更多信息,我建议查看:

  • 如何使用Firebase消息发送一对一消息
  • 如何在不使用XMPP或任何其他脚本的情况下使用FCM发送设备到设备通知

最新更新