Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ios_push_local_notification

 ios_push_local_notification

iOS, Objective-C, Push Notification, Local Notification

WorldDownTown

February 23, 2014
Tweet

More Decks by WorldDownTown

Other Decks in Programming

Transcript

  1. ໨࣍ • Push / Local Notification ͱ͸ʁ • Push Notification

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ
  2. Push / Local Notification ͱ͸ʁ • ܯࠂ΍όφʔ͕දࣔ͞ΕΔ • ΞϓϦέʔγϣϯΞΠίϯʹόοδ͕ͭ͘ •

    α΢ϯυ͕໐Δ Ϣʔβʔʹͱͬͯ͸ Push Notification΋Local Notification΋ ಉ͡෺ʹݟ͑Δ
  3. ໨࣍ • Push / Local Notification ͱ͸ʁ • Push Notification

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ
  4. 1VTI/PUJpDBUJPOͷ࢓૊Έ  1. Apple Push Notification Service (APNs) ͱ͍͏ήʔτ΢ΣΠʹσόΠε Λొ࿥

    2. σόΠε͕APNs͔ΒdeviceTokenΛड͚औΔ 3. ΞϓϦ͕σόΠε͔ΒdeviceTokenΛड͚औΔ 4. deviceTokenΛϓϩόΠμ(ΞϓϦ ӡӦऀͷαʔό౳)ʹొ࿥͢Δ
  5. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // APNsにデバイス登録 UIRemoteNotificationType notificationType

    = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationType]; ! return YES; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *deviceTokenString = deviceToken.description; NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"<>"]; deviceTokenString = [deviceTokenString stringByTrimmingCharactersInSet:characterSet]; deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""]; ! // deviceTokenStringをサーバに送信 } σόΠετʔΫϯ ઃఆॲཧ࣮૷ UIApplicationDelegateͷԼهͷϝιουΛ࣮૷
  6. QBZMPBEͷΧελϚΠζ BMFSUͷࢠϓϩύςΟ Ωʔ ஋ͷܕ આ໌ CPEZ จࣈྻ 1VTI௨஌࣌ͷϝοηʔδ BDUJPOMPDLFZ จࣈྻ

    1VTIىಈϘλϯͷϝοηʔδ MPDLFZ จࣈྻ 1VTI௨஌࣌ͷϩʔΧϥΠζϝοηʔδ -PDBMJ[BCMFTUSJOHTϑΝΠϧ಺ͷΩʔΛ༩͑Δ MPDBSHT จࣈྻͷ഑ྻ MPDLFZ಺ͷॻࣜࢦఆࢠͷ୅ΘΓʹ දࣔ͢Δม਺จࣈྻ஋ MBVODIJNBHF จࣈྻ 1VTI͔Βͷىಈ࣌ʹදࣔ͞ΕΔը૾ͷϑΝΠϧ໊ ࢦఆ͠ͳ͚Ε͹%FGBVMUQOH
  7. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSMutableDictionary *payload = [launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]

    mutableCopy]; payload[@"type"] = @"not_running"; if (payload) { // アプリの未起動時にプッシュ通知を受け取り、ユーザーがアクションボタンをタップしたとき // payload[@"key"] -> value [self startWithPayload:payload]; } return YES; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSMutableDictionary *payload = userInfo.mutableCopy; if (application.applicationState == UIApplicationStateActive) { // フォアグラウンドでプッシュ通知を受け取ったとき payload[@"type"] = @"active"; } else { // アプリがサスペンド中にプッシュ通知を受け取り、ユーザーがアクションボタンをタップしたとき payload[@"type"] = @"background"; } // payload[@"key"] -> value [self startWithPayload:payload]; } UIApplicationDelegateͷԼهͷϝιουΛ࣮૷ QBZMPBEͷΧελϚΠζ
  8. σόΠετʔΫϯઃఆͷ λΠϛϯά - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // APNsにデバイス登録

    UIRemoteNotificationType notificationType = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationType]; ! return YES; } Α͘ݟΔαϯϓϧ
  9. ໨࣍ • Push / Local Notification ͱ͸ʁ • Push Notification

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ
  10. ௨஌ઃఆ UILocalNotification *notification = [UILocalNotification new]; notification.alertBody = @"UILocalNotificationのテスト"; //

    アラートメッセージ notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(60 * 3)]; // 3分後に通知 notification.applicationIconBadgeNumber = 1; // アプリアイコンのバッジ数 notification.timeZone = [NSTimeZone localTimeZone]; // タイムゾーン notification.alertAction = @"起動して!"; // 起動ボタンのテキスト notification.hasAction = NO; // YESならアラートからアプリを起動しない notification.soundName = UILocalNotificationDefaultSoundName; // 通知音 notification.alertLaunchImage = @"push.png"; // 起動時の画像指定しなければDefault.png notification.userInfo = @{@"id":@1234}; // カスタムプロパティ [[UIApplication sharedApplication] scheduleLocalNotification:notification]; // 通知の予約 Push Notificationʹൺ΂ͨΒྦ͕ग़Δ΄Ͳ؆୯! Push Notificationͱಉ͡Α͏ͳࡉ͔͍Φϓγϣϯ΋͋Δ
  11. ௨஌Ωϟϯηϧ NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications; for (UILocalNotification *notification in

    notifications) { if ([notification.userInfo[@"id"] integerValue] == 1234) { [[UIApplication sharedApplication] cancelLocalNotification:notification]; } } ΧϨϯμʔΞϓϦͰεέδϡʔϧΛ࡟আ࣌͠ͳͲɺ! ௨஌ΛΩϟϯηϧ͢Δ͜ͱ͕Ͱ͖Δ [[UIApplication sharedApplication] cancelAllLocalNotifications]; શͯͷ௨஌ΛΩϟϯηϧ ಛఆͷ௨஌ΛΩϟϯηϧ͢Δʹ͸userInfoͷ৘ใ͔ΒΩϟϯηϧ͢Δ௨஌Λಛఆ͢Δ
  12. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

    if (notification) { // アプリの未起動時に通知を受け取り、ユーザーがアクションボタンをタップしたとき // notification.userInfo[@"key"] -> value [self startWithNotification:notification]; } return YES; } - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if (application.applicationState == UIApplicationStateActive) { // フォアグラウンドで通知を受け取ったとき } else { // アプリがサスペンド中に通知を受け取り、ユーザーがアクションボタンをタップしたとき } // notification.userInfo[@"key"] -> value [self startWithNotification:notification]; } UIApplicationDelegateͷԼهͷϝιουΛ࣮૷ (Push Notificationͱ΄΅ಉ͡) ىಈ࣌ͷ੍ޚ
  13. ໨࣍ • Push / Local Notification ͱ͸ʁ • Push Notification

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ
  14. 1VTI-PDBM/PUJpDBUJPOൺ ֱ 1VTI/PUJpDBUJPO -PDBM/PUJpDBUJPO ઃఆ؀ڥߏங ϓϩόΠμ؀ڥ΍ 1SPWJTJPOJOHͷઃఆ͕ ඞཁ ωΠςΟϒͷίʔυ ͷΈ

    ௨஌λΠϛϯά ϓϩόΠμଆ͔Β೚ ҙʹมߋՄೳ εέδϡʔϦϯά͕Մೳͩ ͕ɺ ௨஌Λઃఆ͢Δͷ͸ΞϓϦى ಈத ௨஌ςΩετ ϓϩόΠμଆ͔Β೚ ҙʹมߋՄೳ ΞϓϦ಺෦Ͱઃఆ มߋ͢Δʹ͸ΞϓϦͷόʔδ ϣϯΞοϓ͕ඞཁ
  15. ໨࣍ • Push / Local Notification ͱ͸ʁ • Push Notification

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ
  16. UIApplicationDelegateͷԼهͷϝιουΛ࣮૷ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { //

    コンテンツのダウンロード処理など ! if (success) { if (hasData) { // データ取得成功 completionHandler(UIBackgroundFetchResultNewData); } else { // データなし completionHandler(UIBackgroundFetchResultNoData); } } else { // データ取得失敗 completionHandler(UIBackgroundFetchResultFailed); } } 3FNPUFOPUJpDBUJPOT ઃఆํ๏ 
  17. { "aps":{ "alert":"hello", "badge":1, "sound":"default", "content-available":1 } } ϓϩόΠμ͕ૹ৴͢ΔQBZMPBEʹzDPOUFOUBWBJMBCMFzΛ௥Ճ͢Δͱ 3FNPUFOPUJpDBUJPOT

    QBZMPBE - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler ͕࣮ߦ͞ΕΔ
  18. ໨࣍ • Push / Local Notification ͱ͸ʁ • Push Notification

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ