[英]Android Java App - When firing a notification the sender gets it but other devices running the app do not
我已經閱讀了在線文檔並遵循了幾個教程,代碼在發送通知時“有效”,但它只顯示在創建它的設備上。 根本沒有其他設備收到它。
用戶成群結隊,將它們視為聊天室,他們都可以在其中看到彼此的聊天。 顯然我想向除發件人以外的所有人添加新消息通知。
到目前為止我所擁有的:
創建時
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);
}
然后當用戶向群組發送新消息時:
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);
}
我知道我做錯了什么,而且可能很小。 我究竟做錯了什么?
我誤解了通知的工作原理。 我以為發送消息時會向其他設備發送通知,但應用程序會在收到消息時為其自己的設備創建通知。! 不是我最好的時刻。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.