簡體   English   中英

如何確定用戶是否曾見過要求推送通知權限的對話框(ios)

[英]How can I determine if a user has ever seen the dialog asking for push notification permission (ios)

我知道enableremotenotificationtypes,但它沒有幫助我,因為如果我收到enabledremotenotificationtypes == UIRemoteNotificationTypeNone,我無法判斷用戶是否有1.接受推送通知一次,但后來通過設置后關閉它或2.拒絕推送通知或3.從未見過要求許可的藍色對話框。 我需要一種方法來區分這三種情況。

任何幫助將非常感激。

解決方案有點像黑客,但確實有效。 您需要為兩個不同的notificationSettings調用registerUserNotificationSettings - 一個沒有notificationCategory,另一個有notificationCategory:

    //Request notification permission
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

//Request notification permission again, but with a category with no actions
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = @"com.xyz.markNotificationPopupShownCategoryIdentifier";

UIUserNotificationSettings *notificationSettingsWithCategory = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:[NSSet setWithObject:category]];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettingsWithCategory];

應用程序委托中的didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings方法將被調用兩次,無論用戶在權限通知中的答案如何,在第二次調用之后,當前通知設置將包含該類別。 只要類別計數大於0,您就可以確定已顯示通知權限對話框:

if ([UIApplication sharedApplication].currentUserNotificationSettings.categories.count > 0) {
    NSLog(@"Notifications permissions has been asked");
} else {
    NSLog(@"Notifications permissions hasn't been asked");
}

這是我處理這種情況的方式 - 我是一個新手,所以它可能不是最佳的,但它適用於我。 創建一個int屬性pushNotificationSeen 如果用戶看到對話並拒絕它,則將pushNotificationSeen設置為1.如果用戶看到對話並接受它,則將pushNotificationSeen設置為2.然后,在下一行代碼中,調用這樣的函數(在代碼中的其他位置定義) ):

-(void)saveData
{
if (self.pushNotificationSeen)
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:self.pushNotificationSeen forKey:@"seen?"];
    [defaults synchronize];
}
}

然后viewDidLoad添加到viewDidLoad

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.pushNotificationSeen = [defaults integerForKey:@"seen?"];

此時,您可以通過檢查self.pushNotificationSeen是0,1還是2來確定用戶已執行或未執行的操作。

我希望這是足夠的信息 - 我的睡眠不是很多。 如果我一直困惑,請告訴我,我可以澄清一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM