简体   繁体   中英

Android Java App - When firing a notification the sender gets it but other devices running the app do not

I have read the online docs and followed several tutorials, the code "works" in that a notification is sent, however it just shows up on the device which created it. No other devices receive it at all.

users are in groups, think of them as chat rooms where they can all see each others chats. Obviously I want to add notification of new messages to everyone except the sender.

What I have so far:

onCreate

notificationManager = NotificationManagerCompat.from(this);
notificationChannelId = "FamilyOnlyChat-" + familyKey;
builder = new NotificationCompat.Builder(this, notificationChannelId );
createNotificationChannel();

createNotificationChannel()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  CharSequence name = getString(R.string.channel_name);
  String description = getString(R.string.channel_description);
  int importance = NotificationManager.IMPORTANCE_DEFAULT;
  NotificationChannel channel = new 
  NotificationChannel(notificationChannelId, name, importance);
  channel.setDescription(description);
  // Register the channel with the system; you can't change the importance
  // or other notification behaviors after this
  NotificationManager notificationManager = 
  getSystemService(NotificationManager.class);
  notificationManager.createNotificationChannel(channel);
}

and then when a user sends a new message to the group:

private void sendNotification(ChatMessage message) {
 // Create an explicit intent for an Activity in your app
 Intent intent = new Intent(this, ChatActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

 createNotification(message);
 builder.setContentIntent(pendingIntent).setAutoCancel(true);
 notificationManager.notify(NotificationID.getID(), builder.build());
}

private void createNotification(ChatMessage message) {
 builder.setSmallIcon(R.drawable.ic_launcher)
  .setContentTitle("FamilyChat: " + message.getMessageUser())
  .setContentText(message.getMessageText())
  .setPriority(NotificationCompat.PRIORITY_DEFAULT);
}

I know I am doing something wrong and it might be small. What am I doing wrong?

I misunderstood how notifications worked. I thought a notification was sent to other devices when a message was sent, but instead the app creates a notification for it's own device when a message is received.! Not my finest moment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM