如何在收到通知时自动在 Android 上添加新的通知渠道?

发布于 2025-01-10 08:36:18 字数 2174 浏览 1 评论 0原文

如果用户单击通知并打开应用程序,我就可以做到这一点。 我正在使用通知发送频道名称和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文