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

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

我正在使用 trigger.io 制作一个支持推送通知的应用程序。这很好用。如果我全局禁用推送通知,该应用程序将停止接收任何推送通知。 trigger.io 中是否有任何命令可以确定用户是否通过设置为我的应用程序启用或禁用了他们的推送通知。

你可以使用

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

这将 return 他们订阅的频道列表,如果您的频道不存在则他们没有订阅,并在回调中执行您想要的操作(订阅等)

您实际上可以从 iOS 获取推送设置,但是您需要将其实现为您自己的 native module。我在基于 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];
}

不要被那一堆代码吓到。前半部分用于 >=iOS8,下半部分用于以下所有内容。它基本上会告诉您用户为您的应用激活了哪种类型的通知。

return 值将是一个由 "badge"、"alert" and/or "sound" 组成的数组。

如果用户禁用了您应用的推送,则该数组将为空。


PS:我不擅长Objective-C,因为上面的代码显然可以做得更好。但是,它对我仍然有效 ;)


编辑: 我刚刚意识到您也询问了 Android。我的回答只与 iOS 有关。根据此 post Android 4.1: How to check notifications are disabled for the application? 无法在 Android.

上获得该信息