2014-09-19 11 views
19

Sto sviluppando un'applicazione iOS8 che supporta la notifica interattiva. Ma non ho chiarezza sulle funzionalità supportate dalla notifica interattiva e su come inviare/gestire notifiche interattive. Se qualcuno può dare un esempio, sarebbe molto utile per me.Come implementare la notifica interattiva iOS8

Grazie in anticipo :)

risposta

35
  1. In primo luogo è necessario creare l'azione di notifica.
  2. In secondo luogo è necessario creare la categoria di notifica e impostarne le azioni. È possibile impostare due contesti. UIUserNotificationActionContextDefault o UIUserNotificationActionContextMinimal
  3. Terzo è necessario creare l'impostazione di notifica e assegnare le categorie di cui sopra
  4. Quarto passo sarebbe quello di creare la notifica locale e assegnare l'identificativo della categoria.

UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init]; 
notificationAction1.identifier = @"Accept"; 
notificationAction1.title = @"Accept"; 
notificationAction1.activationMode = UIUserNotificationActivationModeBackground; 
notificationAction1.destructive = NO; 
notificationAction1.authenticationRequired = NO; 

UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init]; 
notificationAction2.identifier = @"Reject"; 
notificationAction2.title = @"Reject"; 
notificationAction2.activationMode = UIUserNotificationActivationModeBackground; 
notificationAction2.destructive = YES; 
notificationAction2.authenticationRequired = YES; 

UIMutableUserNotificationAction *notificationAction3 = [[UIMutableUserNotificationAction alloc] init]; 
notificationAction3.identifier = @"Reply"; 
notificationAction3.title = @"Reply"; 
notificationAction3.activationMode = UIUserNotificationActivationModeForeground; 
notificationAction3.destructive = NO; 
notificationAction3.authenticationRequired = YES; 

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init]; 
notificationCategory.identifier = @"Email"; 
[notificationCategory setActions:@[notificationAction1,notificationAction2,notificationAction3] forContext:UIUserNotificationActionContextDefault]; 
[notificationCategory setActions:@[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextMinimal]; 

NSSet *categories = [NSSet setWithObjects:notificationCategory, nil]; 

UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; 
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories]; 

[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; 

UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; 
localNotification.alertBody = @"Testing"; 
localNotification.category = @"Email"; // Same as category identifier 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
+0

La ringrazio molto @Sourav Gupta. Ma per quanto riguarda la notifica remota? –

+2

Lo stesso codice funzionerà anche per la notifica remota. Hai solo bisogno di aggiungere la chiave "categoria" nel payload push. la chiave "categoria" dovrebbe avere lo stesso valore identificativo che stai definendo nel tuo codice. –

+0

Sorry Sourav Non capisco il concetto. Potresti per favore elaborare la tua risposta? Quando invio una notifica remota, il carico utile dovrebbe contenere tutti i dettagli? –

11

Vorrei estendere la risposta di Sourav Gupta un po '. Una volta che hai fatto fino a ciò che Sourav ha spiegato, devi implementare i delegati per ricevere le azioni di notifica push. I delegati sono,

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler { 

// Handle actions of local notifications here. You can identify the action by using "identifier" and perform appropriate operations 

    if(completionHandler != nil) //Finally call completion handler if its not nil 
     completionHandler(); 
} 

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler { 

// Handle actions of remote notifications here. You can identify the action by using "identifier" and perform appropriate operations 

    if(completionHandler != nil) //Finally call completion handler if its not nil 
     completionHandler(); 
} 

è possibile fare riferimento campione notifica payload remoto qui iOS8 Push notification Payload

Problemi correlati