如何在收到通知时自动在 Android 上添加新的通知渠道?
如果用户单击通知并打开应用程序,我就可以做到这一点。 我正在使用通知发送频道名称和 ID,然后在 onCreate 中使用它
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = getIntent().getExtras().getString("channelId");
String channelName = getIntent().getExtras().getString("channelName");
NotificationChannel channel = new NotificationChannel(channelId, channelName,
NotificationManager.IMPORTANCE_HIGH
);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
}
,或者如果应用程序在前台,则调用 onMessageReceived,所以我可以这样做:
String channelName = remoteMessage.getData().get("channelName");
String channelId = remoteMessage.getData().get("channelId");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId , channelName ,
NotificationManager.IMPORTANCE_HIGH
);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
}
但是如果应用程序关闭或在后台并且用户没有单击在通知上,我无法创建新通道,
这是 python 端:
def sendMulticast(notificationTitle, notificationMessage, notificationLink, notificationTokens, imageLink, channelName, channelId):
if len(notificationTokens)>0:
try :
message = messaging.MulticastMessage(
tokens = list(dict.fromkeys(notificationTokens)),
data={"link":notificationLink, "channelName":channelName, "channelId":channelId},
android=messaging.AndroidConfig(
priority='high',
notification=messaging.AndroidNotification(title=notificationTitle,
body=notificationMessage,
image=imageLink,
color='#ff0000', default_light_settings="true", priority="max", default_vibrate_timings="true", default_sound ="true", channel_id=channelId
),
),
)
response = messaging.send_multicast(message)
print('Successfully sent message:', response)
except Exception as e:
print(str(e))
i can do it if the user clicks on the notification and opens the app.
i'm sending a channel name and id with my notifications then using this in my onCreate
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = getIntent().getExtras().getString("channelId");
String channelName = getIntent().getExtras().getString("channelName");
NotificationChannel channel = new NotificationChannel(channelId, channelName,
NotificationManager.IMPORTANCE_HIGH
);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
}
or if the app is foreground, onMessageReceived is called so i can just do:
String channelName = remoteMessage.getData().get("channelName");
String channelId = remoteMessage.getData().get("channelId");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId , channelName ,
NotificationManager.IMPORTANCE_HIGH
);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
}
but if the app is closed or in the background and user doesn't click on the notification, i can't create the new channel
here's the python side:
def sendMulticast(notificationTitle, notificationMessage, notificationLink, notificationTokens, imageLink, channelName, channelId):
if len(notificationTokens)>0:
try :
message = messaging.MulticastMessage(
tokens = list(dict.fromkeys(notificationTokens)),
data={"link":notificationLink, "channelName":channelName, "channelId":channelId},
android=messaging.AndroidConfig(
priority='high',
notification=messaging.AndroidNotification(title=notificationTitle,
body=notificationMessage,
image=imageLink,
color='#ff0000', default_light_settings="true", priority="max", default_vibrate_timings="true", default_sound ="true", channel_id=channelId
),
),
)
response = messaging.send_multicast(message)
print('Successfully sent message:', response)
except Exception as e:
print(str(e))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论