我正在使用 trigger.io 来制作支持推送通知的应用程序。这工作正常。如果我全局禁用推送通知,应用程序将停止接收任何推送通知。trigger.io 中是否有任何命令可以确定用户是否通过设置为我的应用程序启用或禁用了推送通知。
forge.parse.push.subscribedChannels(success, error
这将返回他们订阅的频道列表,如果您的频道不在那里,那么他们没有订阅,并在回调中做你想要的(订阅等)
您实际上可以从iOS获取推送设置,但是您需要将其实现为自己的本机模块。我在基于 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,后半部分用于下面的所有内容。它基本上告诉你用户已为您的应用激活了哪种类型的通知。
返回值将是一个由"徽章"、"警报"和/或"声音"组成的数组。
如果用户禁用了应用的推送,则数组将为空。
PS:我不擅长Objective-C,因为上面的代码显然可以做得更好。但是,它仍然对我有用;)
编辑:我刚刚意识到你也问过Android。我的回答只与iOS有关。根据这篇文章 Android 4.1:如何检查应用程序的通知是否被禁用?在Android上无法获得这些信息。