ios_push_local_notification

 ios_push_local_notification

iOS, Objective-C, Push Notification, Local Notification

3d5e25f0ab058f8c44419c1ae41be86e?s=128

WorldDownTown

February 23, 2014
Tweet

Transcript

  1. 1VTI-PDBM /PUJpDBUJPO ͱͦͷपล 2014/2/23 ঙ࢘ ܒี

  2. ࣗݾ঺հ • ঙ࢘ ܒี • twitter/github: WorldDownTown • iOSྺ1೥ iQONͷiOSΤϯδχΞ

  3. J20/

  4. J20/ ϑΝογϣϯࡶࢽΛݟΔΑ͏ʹτϨϯυ͕Θ͔ͬͯɺ ݟ͍ͯΔ΋ͷ͕௨ൢͰ͖ΔແྉͷϑΝογϣϯ ίʔσΟωʔτΞϓϦ AppStoreͷ2012೥ϕετΞϓϦʹબग़ʂ

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

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ
  6. Push / Local Notification ࢖ͬͯ·͔͢ʁ

  7. Push / Local Notification ͱ͸ʁ

  8. Push / Local Notification ͱ͸ʁ ϩʔΧϧ௨஌͓Αͼϓογϡ௨஌͕ղܾ͢Δ໰୊ ϑΥΞάϥ΢ϯυͰ͸ɺ౓ʹͭͷΞϓϦέʔγϣϯ͔͠ΞΫςΟϒʹͳΕ·ͤΜɻ ଟ͘ͷΞϓϦέʔγϣϯ͸ɺ࣌ؒϕʔε·ͨ͸૬ޓ઀ଓ͞Εͨ؀ڥͰಈ࡞͍ͯ͠Δͨ ΊɺΞϓϦέʔγϣϯ͕ϑΥΞάϥ΢ϯυʹͳ͍ͱ͖ʹɺϢʔβ͕ؔ৺Λ͍࣋ͬͯΔ Πϕϯτ͕ൃੜ͢ΔՄೳੑ͕͋Γ·͢ɻ

    ϩʔΧϧ௨஌͓Αͼϓογϡ௨஌ʹΑΓɺΞϓϦέʔγϣϯ͸ɺ͜ΕΒͷΠϕϯτ͕ ൃੜͨ͜͠ͱΛϢʔβʹ௨஌Ͱ͖ΔΑ͏ʹͳΓ·͢ɻ ʰ-PDBM͓Αͼ1VTI/PUJpDBUJPOϓϩάϥϛϯάΨΠυʱ
  9. Push / Local Notification ͱ͸ʁ ϩʔΧϧ௨஌͓Αͼϓογϡ௨஌͕ղܾ͢Δ໰୊ ϑΥΞάϥ΢ϯυͰ͸ɺ౓ʹͭͷΞϓϦέʔγϣϯ͔͠ΞΫςΟϒʹͳΕ·ͤΜɻ ଟ͘ͷΞϓϦέʔγϣϯ͸ɺ࣌ؒϕʔε·ͨ͸૬ޓ઀ଓ͞Εͨ؀ڥͰಈ࡞͍ͯ͠Δͨ ΊɺΞϓϦέʔγϣϯ͕ϑΥΞάϥ΢ϯυʹͳ͍ͱ͖ʹɺϢʔβ͕ؔ৺Λ͍࣋ͬͯΔ Πϕϯτ͕ൃੜ͢ΔՄೳੑ͕͋Γ·͢ɻ

    ϩʔΧϧ௨஌͓Αͼϓογϡ௨஌ʹΑΓɺΞϓϦέʔγϣϯ͸ɺ͜ΕΒͷΠϕϯτ͕ ൃੜͨ͜͠ͱΛϢʔβʹ௨஌Ͱ͖ΔΑ͏ʹͳΓ·͢ɻ ʰ-PDBM͓Αͼ1VTI/PUJpDBUJPOϓϩάϥϛϯάΨΠυʱ ར༻཰޲্ʹͭͳ͕Δ
  10. Push / Local Notification ͱ͸ʁ • ܯࠂ΍όφʔ͕දࣔ͞ΕΔ • ΞϓϦέʔγϣϯΞΠίϯʹόοδ͕ͭ͘ •

    α΢ϯυ͕໐Δ Ϣʔβʔʹͱͬͯ͸ Push Notification΋Local Notification΋ ಉ͡෺ʹݟ͑Δ
  11. Push / Local Notification ͱ͸ʁ • ࣮૷ํ๏ͷҧ͍ • ϝϦοτ/σϝϦοτͷҧ͍ ΞϓϦέʔγϣϯʹͱͬͯ͸

    Push NotificationͱLocal Notification͸ ҧ͏෺ʹݟ͑Δ
  12. ໨࣍ • Push / Local Notification ͱ͸ʁ • Push Notification

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ
  13. 1VTI/PUJpDBUJPO

  14. 1VTI/PUJpDBUJPO • iPhone OS 3.0ʙ • αʔό͔ΒiOS୺຤΁௨஌ΛૹΔ • αʔό͔Β೚ҙͷϝοηʔδ/λΠϛϯά Ͱૹ৴Մೳ

  15. 1VTI/PUJpDBUJPOͷ࢓૊Έ  1. Apple Push Notification Service (APNs) ͱ͍͏ήʔτ΢ΣΠʹσόΠε Λొ࿥

    2. σόΠε͕APNs͔ΒdeviceTokenΛड͚औΔ 3. ΞϓϦ͕σόΠε͔ΒdeviceTokenΛड͚औΔ 4. deviceTokenΛϓϩόΠμ(ΞϓϦ ӡӦऀͷαʔό౳)ʹొ࿥͢Δ
  16. 1VTI/PUJpDBUJPOͷ࢓૊Έ  1. ϓϩόΠμ͸deviceTokenͱpayloadΛؚΉ௨஌ύοέʔδΛ࡞੒ ϦϞʔτ௨஌ͷσʔλ͸Ұํ޲ʹྲྀΕΔ 2. ϓϩόΠμ͸ͦͷ௨஌ΛAPNsʹૹ৴ 3. APNs͕ͦͷ௨஌ΛσόΠεʹ഑৴ʢϓογϡʣ

  17. 1VTI/PUJpDBUJPOΛૹΔ ४උ • ϓϩόΠμͷ؀ڥߏங • σόΠετʔΫϯઃఆॲཧ࣮૷

  18. ϓϩόΠμͷ؀ڥߏங লུ

  19. ϓϩόΠμͷ؀ڥߏங ໘౗ͳ؀ڥઃఆΛ͠ͳͯ͘΋͍͍Α͏ʹͳ͖ͬͯ·ͨ͠

  20. - (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ͷԼهͷϝιουΛ࣮૷
  21. ͜ΕͰ४උ׬ྃ ϓϩόΠμ͔Β 1VTIૹ৴Մೳ

  22. Ԡ༻ • payloadͷΧελϜϓϩύςΟ • σόΠετʔΫϯઃఆλΠϛϯά

  23. QBZMPBEͷΧελϚΠζ { "aps":{ "alert":"hello", "badge":1, "sound":"default" } } γϯϓϧͳQBZMPBE

  24. QBZMPBEͷΧελϚΠζ { "aps":{ "alert":{ "body":"hello", "action-loc-key":"ゲームを再開", "launch-image":"push.png" }, "badge":1, "sound":"default"

    } } BMFSUͷࢠϓϩύςΟͰΧελϚΠζͰ͖Δ
  25. QBZMPBEͷΧελϚΠζ BMFSUͷࢠϓϩύςΟ Ωʔ ஋ͷܕ આ໌ CPEZ จࣈྻ 1VTI௨஌࣌ͷϝοηʔδ BDUJPOMPDLFZ จࣈྻ

    1VTIىಈϘλϯͷϝοηʔδ MPDLFZ จࣈྻ 1VTI௨஌࣌ͷϩʔΧϥΠζϝοηʔδ -PDBMJ[BCMFTUSJOHTϑΝΠϧ಺ͷΩʔΛ༩͑Δ MPDBSHT จࣈྻͷ഑ྻ MPDLFZ಺ͷॻࣜࢦఆࢠͷ୅ΘΓʹ දࣔ͢Δม਺จࣈྻ஋ MBVODIJNBHF จࣈྻ 1VTI͔Βͷىಈ࣌ʹදࣔ͞ΕΔը૾ͷϑΝΠϧ໊ ࢦఆ͠ͳ͚Ε͹%FGBVMUQOH
  26. QBZMPBEͷΧελϚΠζ BDUJPOMPDLFZ

  27. QBZMPBEͷΧελϚΠζ { "aps":{ "alert":"hello", "badge":1, "sound":"default" }, "key":"value" } ΞϓϦέʔγϣϯʹLFZWBMVFΛ౉͢͜ͱ͕Ͱ͖Δ

    Push௨஌ىಈͷϩάΛམͱͨ͠Γ! ىಈ࣌ͷViewControllerΛࢦఆͨ͠ΓͰ͖Δ
  28. - (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ͷΧελϚΠζ
  29. σόΠετʔΫϯઃఆͷ λΠϛϯά - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // APNsにデバイス登録

    UIRemoteNotificationType notificationType = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationType]; ! return YES; } Α͘ݟΔαϯϓϧ
  30. σόΠετʔΫϯઃఆͷ λΠϛϯά ·ͩͲΜͳΞϓϦ͔Α͘Θ͔Βͳ͍ͷʹɺ! ॳճىಈͷλΠϛϯάͰϓογϡͷڐՄ! Λฉ͍ͯ͠·͏ͱڐՄͯ͠΋Β͑ͳ͍…

  31. σόΠετʔΫϯઃఆͷ λΠϛϯά νϡʔτϦΞϧʹ͋ΔΑ͏ʹ! application:didFinishLaunchingWithOptions: ಺Ͱ! registerForRemoteNotificationTypesΛίʔϧͤͣɺ! ϓογϡ௨஌Λड͚औΔϝϦοτड़΂ͨ͏͑Ͱ! ॳΊͯregisterForRemoteNotificationTypes:Λίʔϧ͢Δ iQONͰ΋PUSHઃఆ཰͕޲্!

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

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ
  33. -PDBM/PUJpDBUJPO • iOS 4.0ʙ • ໘౗ͳϓϩόΠμઃఆ͕ෆཁ • λΠϚʔ΍܁Γฦ͠Ͱ௨஌ΛදࣔͰ͖Δ • ΞϓϦ͕OSʹରͯ͠௨஌ͷεέδϡʔϧ

    ΍ςΩετΛઃఆ͢Δ
  34. ௨஌ઃఆ 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ͱಉ͡Α͏ͳࡉ͔͍Φϓγϣϯ΋͋Δ
  35. ௨஌Ωϟϯηϧ NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications; for (UILocalNotification *notification in

    notifications) { if ([notification.userInfo[@"id"] integerValue] == 1234) { [[UIApplication sharedApplication] cancelLocalNotification:notification]; } } ΧϨϯμʔΞϓϦͰεέδϡʔϧΛ࡟আ࣌͠ͳͲɺ! ௨஌ΛΩϟϯηϧ͢Δ͜ͱ͕Ͱ͖Δ [[UIApplication sharedApplication] cancelAllLocalNotifications]; શͯͷ௨஌ΛΩϟϯηϧ ಛఆͷ௨஌ΛΩϟϯηϧ͢Δʹ͸userInfoͷ৘ใ͔ΒΩϟϯηϧ͢Δ௨஌Λಛఆ͢Δ
  36. - (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ͱ΄΅ಉ͡) ىಈ࣌ͷ੍ޚ
  37. -PDBM/PUJpDBUJPOԠ༻ྫ Sleipnir͕νϡʔτϦΞϧͱͯ͠Local Notification! Λར༻͍ͯ͠Δ

  38. -PDBM/PUJpDBUJPOԠ༻ྫ ࣮૷Λ૝૾ͯ͠Έͨ • AppDelegateͷapplicationWillResignActive:Ͱ௨஌ͷઃఆ • notification.soundName = nil; • νϡʔτϦΞϧͱͯ͠఻͍͑ͨػೳΛطʹ࢖ͬͨ͜ͱ͕

    ͋Δ৔߹͸ɺ௨஌ΛΩϟϯηϧ
  39. ໨࣍ • Push / Local Notification ͱ͸ʁ • Push Notification

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

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

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ
  42. #BDLHSPVOE'FUDI

  43. J04·Ͱͷ͓͞Β͍ • ΞϓϦ͕όοΫάϥ΢ϯυʹͳͬͨࡍͷॲཧ͸ Իָ࠶ੜɺҐஔ৘ใऔಘɺVoIPͷΈՄೳ • ͦΕҎ֎͸UIApplicationͷ beginBackgroundTaskWithExpirationHandler: Ͱ όοΫάϥ΢ϯυʹߦ͔ͬͯΒ10෼͚ؒͩॲཧ ͕Մೳͩͬͨɻ10෼ޙʹ͸αεϖϯυ͢Δ

  44. #BDLHSPVOE'FUDI • iOS7ͰBackground Modeʹ௥Ճ͞Εͨ • 10෼ͷ੍ݶ͸ͳ͘ͳͬͨ • OS͕࠷దͱ൑அͨ͠λΠϛϯάͰόοΫάϥ΢ ϯυॲཧΛߦ͏ •

    ݺͼग़͠λΠϛϯά͸ϢʔβʔͷΞϓϦͷར༻ ঢ়گΛΈֶͯश͢Δ
  45. 3FNPUF/PUJpDBUJPOT • iOS7ͰBackground Modeʹ௥Ճ͞Εͨ • Push NotificationΛΩʔʹόοΫάϥ΢ϯυͰॲ ཧΛ࣮ߦ͢Δ͜ͱ͕Ͱ͖Δ

  46. 3FNPUFOPUJpDBUJPOT ઃఆํ๏  XcodeͷCapabilitiesઃఆͰBackgroundMode > Remote notificationsʹνΣοΫ

  47. 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 ઃఆํ๏ 
  48. { "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 ͕࣮ߦ͞ΕΔ
  49. { "aps":{ "content-available":1 } } 4JMFOU1VTI QBZMPBEͷϝοηʔδΛࢦఆͤͣʹzDPOUFOUBWBJMBCMFz͚ͩࢦఆ͢Δͱɺ ௨஌͕දࣔ͞Εͣʹɺॲཧ͚࣮ͩߦ͞ΕΔ 4JMFOU1VTI

  50. ஫ҙ఺ • όοΫά΢ϯυͰΞϓϦ͕ىಈͯ͠ ͍ͳ͍ͱॲཧ͸ݺͼग़͞Εͳ͍ • iOSͷઃఆͰΞϓϦ͝ͱʹόοΫάϥ ΢ϯυॲཧͷڐՄΛऔΓফ͢͜ͱ͕ Ͱ͖Δ ඞਢػೳͱͯ͠ఏڙ͢Δͷ͸ةݥ

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

    • Local Notification • Push / Local Notificationൺֱ • iOS7ͷػೳ • ·ͱΊ
  52. ·ͱΊ 1VTI/PUJpDBUJPO • ॳճͷઃఆ΍؀ڥߏங͕໘౗ • ͦΕͰ΋Ϣʔβʔ࠶ར༻཰ΛߴΊΔʹ͸ ඞਢػೳ

  53. ·ͱΊ -PDBM/PUJpDBUJPO • ઃఆ͕؆୯ • ΧϨϯμʔ΍ToDoΞϓϦͳͲϢʔβʔͱ ͷίϛϡχέʔγϣϯ͕ແ͍ΞϓϦʹ͸ ޲͍ͯΔ

  54. ·ͱΊ #BDLHSPVOE'FUDI • େ͖ͳσʔλΛಡΈࠐΉΑ͏ͳΞϓϦ͸ Pushͱ૊Έ߹ΘͤΔͱ͍͍͔΋ • όοΫάϥ΢ϯυͰ͔͠ಈ͔ͳ͍ͷͰɺ ඞਢػೳͱ࣮ͯ͠૷͢Δͷ͸ةݥ

  55. 8"/5&% https://www.wantedly.com/projects/5530 iOS(ʹݶΒͣ) ΤϯδχΞืूதʂ