简体   繁体   English

我可以检测用户是否在trigger.io上全局禁用推送通知吗?

[英]Can i detect if the user disables push notification globally on trigger.io?

I am using trigger.io to make an app that supports push notifications. 我正在使用trigger.io制作一个支持推送通知的应用程序。 This works fine. 这很好。 If I disable the push notification globally, the app will stop receive any push notification. 如果我全局禁用了推送通知,则该应用将停止接收任何推送通知。 Are there any commands in trigger.io that can determine if the user has, via settings, enabled or disabled their push notifications for my application. trigger.io中是否有任何命令可以确定用户是否通过设置为我的应用程序启用或禁用了其推送通知。

You can use 您可以使用

forge.parse.push.subscribedChannels(success, error forge.parse.push.subscribedChannels(成功,错误

This will return the list of channels they are subscribed to, if your channel is not there then they are not subscribed and in the callback do what you want ( subscribe etc ) 这将返回他们订阅的频道列表,如果您的频道不存在,则他们没有订阅,并且在回调中执行您想要的操作(订阅等)

You can actually get the push setting from iOS, however you'd need to implement this as your own native module . 实际上,您可以从iOS获得推送设置,但是您需要将其实现为自己的本机模块 I'm using this in production in Trigger.io-based apps: 我在基于Trigger.io的应用程序的生产环境中使用了此功能:

+ (void)getPushStatus:(ForgeTask*)task {

    NSArray *pushStatus;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
        UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
        UIUserNotificationType notificationTypes = settings.types;

        if (notificationTypes == UIUserNotificationTypeBadge) {
            pushStatus = [NSArray arrayWithObjects: @"badge", nil];
        } else if (notificationTypes == UIUserNotificationTypeAlert) {
            pushStatus = [NSArray arrayWithObjects: @"alert", nil];
        } else if (notificationTypes == UIUserNotificationTypeSound) {
            pushStatus = [NSArray arrayWithObjects: @"sound", nil];
        } else if (notificationTypes == (UIUserNotificationTypeBadge | UIUserNotificationTypeAlert)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"alert", nil];
        } else if (notificationTypes == (UIUserNotificationTypeBadge | UIUserNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"sound", nil];
        } else if (notificationTypes == (UIUserNotificationTypeAlert | UIUserNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"alert", @"sound", nil];
        } else if (notificationTypes == (UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"alert", @"sound", nil];
        } else {
            // notificationTypes == UIUserNotificationTypeNone
            pushStatus = [[NSArray alloc] init];
        }
    } else {
        UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

        if (notificationTypes == UIRemoteNotificationTypeBadge) {
            pushStatus = [NSArray arrayWithObjects: @"badge", nil];
        } else if (notificationTypes == UIRemoteNotificationTypeAlert) {
            pushStatus = [NSArray arrayWithObjects: @"alert", nil];
        } else if (notificationTypes == UIRemoteNotificationTypeSound) {
            pushStatus = [NSArray arrayWithObjects: @"sound", nil];
        } else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"alert", nil];
        } else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"sound", nil];
        } else if (notificationTypes == (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"alert", @"sound", nil];
        } else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"alert", @"sound", nil];
        } else {
            // notificationTypes == UIRemoteNotificationTypeNone
            pushStatus = [[NSArray alloc] init];
        }
    }

    [task success:pushStatus];
}

Don't be frightened by that pile of code. 不要被那堆代码吓到。 The first half is for >=iOS8, the second half for everything below. 上半部分用于> = iOS8,下半部分用于以下所有内容。 It basically tells you which type of notifications the user has activated for your app. 它基本上告诉您用户已为您的应用激活了哪种通知类型。

The return value will be an array consisting of "badge", "alert" and/or "sound". 返回值将是一个由“徽章”,“警报”和/或“声音”组成的数组。

If the user disabled push for your app, the array will be empty. 如果用户禁用了对您的应用程序的推送,则该数组将为空。


PS: I'm not good at Objective-C, as the code above could obviously be done better. PS:我不擅长Objective-C,因为上面的代码显然可以做得更好。 However, it still works for me ;) 但是,它仍然对我有用;)


EDIT: I've just realized that you also asked about Android. 编辑:我刚刚意识到,您还询问了Android。 My answer is only relevant for iOS. 我的答案仅与iOS有关。 According to this post Android 4.1: How to check notifications are disabled for the application? 根据这篇文章Android 4.1:如何检查该应用程序的通知已禁用? it's not possible to get that information on Android. 无法在Android上获取该信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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