2014-06-13 16 views
13

Ich möchte eine Warnung anzeigen, wenn die lokale Benachrichtigung ausgelöst wird, aber dafür muss ich um Erlaubnis fragen, wie es mir sagt, wenn ich die App auf meinem iPhone starte :Benutzer um Erlaubnis bitten, beim Auslösen lokale Benachrichtigung zu zeigen

Der Versuch, eine lokale Meldung {Feuer date = freitag 13. Juni 2014 12 h 10 min 27 s Mitteleuropäische Sommerzeit, Zeitzone = (null) zu planen, wiederholen Intervall = 0, Wiederholungszahl = UILocalNotificationInfiniteRepeatCount, nächste Feuerdatum = Freitag, 13. Juni 2014 12 h 10 min 27 s Mitteleuropäische Sommerzeit, Benutzerinfo = (null)} mit einer Warnung, aber keine Berechtigung vom Benutzer zum Anzeigen von Warnungen erhalten

Wie kann ich das tun? Heres der Code, wie es jetzt ist:

UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:timeUntilNotification]; 
    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.alertBody = @"ZEIT!"; 
    localNotif.alertAction = @"Show me the Timer!"; 
    localNotif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] +1; 


    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
+4

iOS 8 erfordert eine Genehmigung. Siehe http://StackOverflow.com/a/24161903 –

+0

Überprüfen Sie diese: http://StackOverflow.com/Questions/24100313/ask-for-user-permission-to-receive-uilocalnotifications-in-ios-8 –

Antwort

40

diesen Code hinzufügen, es wird eine Warnungsansicht zeigen Benutzer um Erlaubnis zu fragen.

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { 
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge 
                              categories:nil]]; 
} 

Sie diesen Code in der Anwendung hinzufügen: didFinishLaunchingWithOptions; Methode, so dass die App Ihren Benutzer fragen wird, wenn sie die App starten, oder Sie können diesen Code hinzufügen, wenn Sie die lokale Benachrichtigung einstellen, liegt ganz bei Ihnen.

+3

Dies Schnipsel ist großartig, danke! Hinweis für alle: Machen Sie sich Gedanken darüber, wann Sie speziell in Ihrer App dem Benutzer die Berechtigung zum Senden von Benachrichtigungen stellen möchten, da Sie sie beim Öffnen Ihrer App nicht direkt vertreiben möchten. Bauen Sie zuerst etwas Vertrauen auf und fragen Sie dann um Erlaubnis. UX zum Nachdenken :) – LargeGlasses

+1

In swift sieht es wie folgt aus: lassen registerUserNotificationSettings = UIApplication.instancesRespondToSelector ("registerUserNotificationSettings:") wenn registerUserNotificationSettings { var Typen: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound UIApplication.sharedApplication(). RegisterUserNotificationSettings (UIUserNotificationSettings (forTypes: types, categories: nil))} – Morkrom

+0

Dieses Codefragment wird für iOS 8 und höher empfohlen. – samthui7

4

蘇健豪s Antwort ist gut.

In Swift sieht es wie folgt aus:

let registerUserNotificationSettings = UIApplication.instancesRespondToSelector("registerUserNotificationSettings:") 

if registerUserNotificationSettings { 
    var types: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound  
    UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotific‌​ationSettings(forTypes: types, categories: nil)) 
} 

Auch hier sehen: Ask for User Permission to Receive UILocalNotifications in iOS 8

+1

Kann ich eine erneute Anfrage stellen? Wenn der Benutzer die Erlaubnis versehentlich nicht erlaubt, kann ich eine Taste machen und wenn der Benutzer darauf klickt, wird die App erneut nach der Erlaubnis fragen. – TomSawyer

0

Try this für Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
     // Override point for customization after application launch. 
     NSLog(@"didFinishLaunchingWithOptions"); 

     if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { 
      [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]]; 
     } 

     return YES; 
    } 
1
//register notifications 
if([application respondsToSelector:@selector(registerUserNotificationSettings:)]) //ios 8+ 
{ 
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; 
    [application registerForRemoteNotifications]; 
} 
else // ios 7 or less 
{ 
    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge]; 
} 
1

In der zügigen Sprache ...

var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound; 
var setting = UIUserNotificationSettings(forTypes: type, categories: nil); 
UIApplication.sharedApplication().registerUserNotificationSettings(setting); 
UIApplication.sharedApplication().registerForRemoteNotifications(); 
Verwandte Themen