如果用戶允許本地通知,是否沒有簡單的測試方法?在我拒絕發送本地通知後,我在控制檯中發現了一條警告。當相關事件發生時,它表示應用程序試圖發送通知,即使用戶不允許。我想在嘗試顯示通知之前檢查它是否被允許。看到我的情況的評論,我該怎麼做?檢查用戶是否允許本地通知。 iOS 8. Obj-C
我的代碼是:
app = [UIApplication sharedApplication];
-(void)showBackgroundNotification:(NSString *) message {
//check if app is in background and check if local notifications are allowed.
if (app.applicationState == UIApplicationStateBackground /*&& LocalNotificationsAreAllowed*/){
UILocalNotification *note = [[UILocalNotification alloc]init];
note.alertBody = message;
note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
[app scheduleLocalNotification :note];
}
}
我得到提示用戶是否允許像這樣:
UIUserNotificationSettings *settings;
if ([app respondsToSelector:@selector(registerUserNotificationSettings:)])
{
settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotification TypeSound) categories:nil];
[app registerUserNotificationSettings:settings];
}
我不能使用設置對象?
編輯:我想我已經解決了它。這似乎工作。
-(void)showBackgroundNotification:(NSString *) message {
if (app.applicationState == UIApplicationStateBackground && [app currentUserNotificationSettings].types != UIUserNotificationTypeNone){
UILocalNotification *note = [[UILocalNotification alloc]init];
note.alertBody = message;
note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
[app scheduleLocalNotification :note];
}
}
檢查我的答案在這裏:http://stackoverflow.com/a/33779144/1463604 – Nishant