Skip to content

Instantly share code, notes, and snippets.

@pine
Last active April 7, 2024 07:46
Show Gist options
  • Select an option

  • Save pine/8b7e58ff7fa259df17351d959c6118f7 to your computer and use it in GitHub Desktop.

Select an option

Save pine/8b7e58ff7fa259df17351d959c6118f7 to your computer and use it in GitHub Desktop.

Revisions

  1. pine revised this gist Dec 16, 2017. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -168,11 +168,10 @@ manager.notify(1, notification)
    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="480">

    ## リモート通知の仕組み
    前章ではローカル通知の方法を說明しました。次はリモート通知について說明します。
    次はリモート通知について說明します。
    リモート通知は、端末へ通知を送りたいサーバーなどから端末へ通知を送る手段です。

    リモート通知を送りたい場合、端末と直接コネクションを貼るのではなく、Apple や
    が提供するプッシュ通知サーバーに対してプッシュ通知を送信することを依頼します。Apple は [APNs (Apple Push Notification service)](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html)、Google は [GCM](https://developers.google.com/cloud-messaging/)/[FCM](https://firebase.google.com/docs/cloud-messaging/?hl=ja) というサービス名でプッシュ通知サービスを提供しています。APNs や GCM/FCM は端末との TCP コネクションを貼っており、プッシュ通知の配信を一括して代行しています。
    リモート通知を送りたい場合、端末と直接コネクションを貼るのではなく、Apple や Google が提供するプッシュ通知サーバーに対してプッシュ通知を送信することを依頼します。Apple は [APNs (Apple Push Notification service)](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html)、Google は [GCM](https://developers.google.com/cloud-messaging/)/[FCM](https://firebase.google.com/docs/cloud-messaging/?hl=ja) というサービス名でプッシュ通知サービスを提供しています。APNs や GCM/FCM は端末との TCP コネクションを貼っており、プッシュ通知の配信を一括して代行しています。

    Apple や Google が提供するこれらのサービスを直接利用する方法がひとつの方法です。他には、これらのプッシュ通知のサーバーを一段ラップした API を提供するサービスを使う方法があります。

  2. pine revised this gist Dec 16, 2017. 1 changed file with 11 additions and 4 deletions.
    15 changes: 11 additions & 4 deletions AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@
    ということで、この記事では iOS/Android のどちらのサンプルコードも書くこととします。

    なお、この記事では iOS/Android は以下のバージョン以上を対象とします。<br>
    これに満たない端末は IT 文化の発展を妨げているため、今すぐ投げ捨ててください。
    ~~これに満たない端末は IT 文化の発展を妨げているため、今すぐ投げ捨ててください。~~

    - iOS 10
    - Android 5.0.2
    @@ -60,7 +60,7 @@
    *サンプルコード: https://github.com/pine/AdventCalendar_KUT_20171217_LocalPush_iOS*

    iOS でローカル通知を利用するには [User Notifications Framework](https://developer.apple.com/documentation/usernotifications) を利用します。これは iOS 10 以降で利用可能です。<br>
    iOS 9 では別の方法を使う必要がありますが、先述の通りこの記事では対象外なため解説しません
    iOS 9 では別の方法を使う必要がありますが、この記事では解説しません

    まず、プッシュ通知の権限を要求するためのコードを書く必要があります。<br>
    これをアプリ起動時の処理を書くことができる [application(_:didFinishLaunchingWithOptions:)](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622921-application) に記載します。
    @@ -171,11 +171,18 @@ manager.notify(1, notification)
    前章ではローカル通知の方法を說明しました。次はリモート通知について說明します。
    リモート通知は、端末へ通知を送りたいサーバーなどから端末へ通知を送る手段です。

    リモート通知を送りたい場合、端末と直接コネクションを貼るのではなく、Apple や Google が提供するプッシュ通知サーバーに対してプッシュ通知を送信することを依頼します。Apple は [APNs (Apple Push Notification service)](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html)、Google は [GCM](https://developers.google.com/cloud-messaging/)/[FCM](https://firebase.google.com/docs/cloud-messaging/?hl=ja) というサービス名でプッシュ通知サービスを提供しています。APNs や GCM/FCM は端末との TCP コネクションを貼っており、プッシュ通知の配信を一括して代行しています。
    リモート通知を送りたい場合、端末と直接コネクションを貼るのではなく、Apple や
    が提供するプッシュ通知サーバーに対してプッシュ通知を送信することを依頼します。Apple は [APNs (Apple Push Notification service)](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html)、Google は [GCM](https://developers.google.com/cloud-messaging/)/[FCM](https://firebase.google.com/docs/cloud-messaging/?hl=ja) というサービス名でプッシュ通知サービスを提供しています。APNs や GCM/FCM は端末との TCP コネクションを貼っており、プッシュ通知の配信を一括して代行しています。

    Apple や Google が提供するこれらのサービスを直接利用する方法がひとつの方法です。他には、これらのプッシュ通知のサーバーを一段ラップした API を提供するサービスを使う方法があります。

    ## Firebase によるリモート通知
    リモート通知を行うには、Google が公式に提供している [Firebase Cloud Message (FCM)](https://firebase.google.com/docs/cloud-messaging/?hl=ja) を使うのが便利です。Android に対しては直接プッシュ通知を配信、iOS 端末に対しても APNs 経由で配信ができます。

    FCM を使ったプッシュ通知の具体的なコードも書く予定だったのですが、~~間に合わなかったため~~今回は見送ります。
    FCM を使ったプッシュ通知の具体的なコードも書く予定だったのですが、~~間に合わなかったため~~今回は見送ります。

    ## まとめ
    プッシュ通知には、ローカル通知とリモート通知の2種類があります。
    ローカル通知はアプリから直接発火、リモート通知は Google や Apple が提供するサーバーを経由して配信されます。

    プッシュ通知は簡単に実装できるので、ぜひ試してみてください!
  3. pine revised this gist Dec 16, 2017. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -175,4 +175,7 @@ manager.notify(1, notification)

    Apple や Google が提供するこれらのサービスを直接利用する方法がひとつの方法です。他には、これらのプッシュ通知のサーバーを一段ラップした API を提供するサービスを使う方法があります。

    ## Firease によるリモート通知
    ## Firebase によるリモート通知
    リモート通知を行うには、Google が公式に提供している [Firebase Cloud Message (FCM)](https://firebase.google.com/docs/cloud-messaging/?hl=ja) を使うのが便利です。Android に対しては直接プッシュ通知を配信、iOS 端末に対しても APNs 経由で配信ができます。

    FCM を使ったプッシュ通知の具体的なコードも書く予定だったのですが、~~間に合わなかったため~~今回は見送ります。
  4. pine revised this gist Dec 16, 2017. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -169,3 +169,10 @@ manager.notify(1, notification)

    ## リモート通知の仕組み
    前章ではローカル通知の方法を說明しました。次はリモート通知について說明します。
    リモート通知は、端末へ通知を送りたいサーバーなどから端末へ通知を送る手段です。

    リモート通知を送りたい場合、端末と直接コネクションを貼るのではなく、Apple や Google が提供するプッシュ通知サーバーに対してプッシュ通知を送信することを依頼します。Apple は [APNs (Apple Push Notification service)](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html)、Google は [GCM](https://developers.google.com/cloud-messaging/)/[FCM](https://firebase.google.com/docs/cloud-messaging/?hl=ja) というサービス名でプッシュ通知サービスを提供しています。APNs や GCM/FCM は端末との TCP コネクションを貼っており、プッシュ通知の配信を一括して代行しています。

    Apple や Google が提供するこれらのサービスを直接利用する方法がひとつの方法です。他には、これらのプッシュ通知のサーバーを一段ラップした API を提供するサービスを使う方法があります。

    ## Firease によるリモート通知
  5. pine revised this gist Dec 16, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -168,4 +168,4 @@ manager.notify(1, notification)
    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="480">

    ## リモート通知の仕組み
    前章ではローカルプッシュの方法を說明しました
    前章ではローカル通知の方法を說明しました。次はリモート通知について說明します
  6. pine revised this gist Dec 16, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -166,3 +166,6 @@ manager.notify(1, notification)
    ```

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="480">

    ## リモート通知の仕組み
    前章ではローカルプッシュの方法を說明しました。
  7. pine revised this gist Dec 9, 2017. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -137,4 +137,32 @@ Android で ローカル通知を利用するには、サポートライブラ
    *どういった互換処理かはソースコードを参照してください<br>
    https://android.googlesource.com/platform/frameworks/support/+/jb-dev/v4/java/android/support/v4/app/NotificationCompat.java*

    [NotificationCompat.Builder](https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html) を使ってプッシュ通知を行うには、以下のようなコードを記述します。

    ```kotlin
    // Android 8 (Oreo) への対応
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val channel = NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT)
    channel.description = "Default channel"
    manager.createNotificationChannel(channel)
    }

    // プッシュ通知を表示
    val intent = MainActivity.createIntent(this)
    val contentIntent = PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT)

    val notification = NotificationCompat.Builder(this, "default")
    .setSmallIcon(R.drawable.ic_launcher_foreground)
    .setContentTitle("れんちょんからのお知らせ")
    .setContentText("にゃんぱすー")
    .setContentIntent(contentIntent)
    .setAutoCancel(true)
    .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_renchon))
    .build()

    val manager = NotificationManagerCompat.from(this)
    manager.notify(1, notification)
    ```

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="480">
  8. pine revised this gist Dec 9, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -134,6 +134,7 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    Android で ローカル通知を利用するには、サポートライブラリにある [NotificationCompat.Builder](https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html) クラスを利用します。Android のネイティブ API である [Notification.Builder](https://developer.android.com/reference/android/app/Notification.Builder.html) を使ってもできますが、下位互換のあるサポートライブラリを使うのが一般的です。

    *どういった互換処理かはソースコードを参照: [/android/support/v4/app/NotificationCompat.java](https://android.googlesource.com/platform/frameworks/support/+/jb-dev/v4/java/android/support/v4/app/NotificationCompat.java)*
    *どういった互換処理かはソースコードを参照してください<br>
    https://android.googlesource.com/platform/frameworks/support/+/jb-dev/v4/java/android/support/v4/app/NotificationCompat.java*

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="480">
  9. pine revised this gist Dec 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -134,6 +134,6 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    Android で ローカル通知を利用するには、サポートライブラリにある [NotificationCompat.Builder](https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html) クラスを利用します。Android のネイティブ API である [Notification.Builder](https://developer.android.com/reference/android/app/Notification.Builder.html) を使ってもできますが、下位互換のあるサポートライブラリを使うのが一般的です。

    *どういった互換処理かはソースコードを参照: [android.support.v4.app.NotificationCompat.java](https://android.googlesource.com/platform/frameworks/support/+/jb-dev/v4/java/android/support/v4/app/NotificationCompat.java)*
    *どういった互換処理かはソースコードを参照: [/android/support/v4/app/NotificationCompat.java](https://android.googlesource.com/platform/frameworks/support/+/jb-dev/v4/java/android/support/v4/app/NotificationCompat.java)*

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="480">
  10. pine revised this gist Dec 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -134,6 +134,6 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    Android で ローカル通知を利用するには、サポートライブラリにある [NotificationCompat.Builder](https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html) クラスを利用します。Android のネイティブ API である [Notification.Builder](https://developer.android.com/reference/android/app/Notification.Builder.html) を使ってもできますが、下位互換のあるサポートライブラリを使うのが一般的です。

    *具体的にどのような互換コードかは、ソースコードを見ると分かります: https://android.googlesource.com/platform/frameworks/support/+/jb-dev/v4/java/android/support/v4/app/NotificationCompat.java*
    *どういった互換処理かはソースコードを参照: [android.support.v4.app.NotificationCompat.java](https://android.googlesource.com/platform/frameworks/support/+/jb-dev/v4/java/android/support/v4/app/NotificationCompat.java)*

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="480">
  11. pine revised this gist Dec 9, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -132,4 +132,8 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    ## Android によるローカル通知
    *サンプルコード: https://github.com/pine/AdventCalendar_KUT_20171217_LocalPush_Android*

    Android で ローカル通知を利用するには、サポートライブラリにある [NotificationCompat.Builder](https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html) クラスを利用します。Android のネイティブ API である [Notification.Builder](https://developer.android.com/reference/android/app/Notification.Builder.html) を使ってもできますが、下位互換のあるサポートライブラリを使うのが一般的です。

    *具体的にどのような互換コードかは、ソースコードを見ると分かります: https://android.googlesource.com/platform/frameworks/support/+/jb-dev/v4/java/android/support/v4/app/NotificationCompat.java*

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="480">
  12. pine revised this gist Dec 9, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -130,5 +130,6 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/9c84d8ed3c0906ab4c3c67aa913aa013a43c8733/iPhone_LocalPush.png" width="480">

    ## Android によるローカル通知
    *サンプルコード: https://github.com/pine/AdventCalendar_KUT_20171217_LocalPush_Android*

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="480">
  13. pine revised this gist Dec 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -127,7 +127,7 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    実行すると、下記のように通知が表示されます。

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/9c84d8ed3c0906ab4c3c67aa913aa013a43c8733/iPhone_LocalPush.png" width="621">
    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/9c84d8ed3c0906ab4c3c67aa913aa013a43c8733/iPhone_LocalPush.png" width="480">

    ## Android によるローカル通知

  14. pine revised this gist Dec 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -131,4 +131,4 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    ## Android によるローカル通知

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="720">
    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="480">
  15. pine revised this gist Dec 9, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -129,4 +129,6 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/9c84d8ed3c0906ab4c3c67aa913aa013a43c8733/iPhone_LocalPush.png" width="621">

    ## Android によるローカル通知
    ## Android によるローカル通知

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/34ff0eeccb10171ed3267e93bd1dc5fea7d462b9/Android_LocalPush.png" width="720">
  16. pine revised this gist Dec 9, 2017. 1 changed file with 0 additions and 0 deletions.
    Binary file added Android_LocalPush.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  17. pine revised this gist Dec 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@
    - Android 5.0.2

    ## iOS によるローカル通知
    *サンプルコード: https://github.com/pine/AdventCalendarKUT_20171217_LocalPush_iOS*
    *サンプルコード: https://github.com/pine/AdventCalendar_KUT_20171217_LocalPush_iOS*

    iOS でローカル通知を利用するには [User Notifications Framework](https://developer.apple.com/documentation/usernotifications) を利用します。これは iOS 10 以降で利用可能です。<br>
    iOS 9 では別の方法を使う必要がありますが、先述の通りこの記事では対象外なため解説しません。
  18. pine revised this gist Dec 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@
    - Android 5.0.2

    ## iOS によるローカル通知
    *サンプルコード: https://github.com/pine/AdventCalendarKUT_20171217_LocalPush*
    *サンプルコード: https://github.com/pine/AdventCalendarKUT_20171217_LocalPush_iOS*

    iOS でローカル通知を利用するには [User Notifications Framework](https://developer.apple.com/documentation/usernotifications) を利用します。これは iOS 10 以降で利用可能です。<br>
    iOS 9 では別の方法を使う必要がありますが、先述の通りこの記事では対象外なため解説しません。
  19. pine revised this gist Dec 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@
    - Android 5.0.2

    ## iOS によるローカル通知
    *サンプルコード: XXX*
    *サンプルコード: https://github.com/pine/AdventCalendarKUT_20171217_LocalPush*

    iOS でローカル通知を利用するには [User Notifications Framework](https://developer.apple.com/documentation/usernotifications) を利用します。これは iOS 10 以降で利用可能です。<br>
    iOS 9 では別の方法を使う必要がありますが、先述の通りこの記事では対象外なため解説しません。
  20. pine revised this gist Dec 9, 2017. 1 changed file with 0 additions and 0 deletions.
    Binary file removed Welcome_to_Xcode.png
    Binary file not shown.
  21. pine revised this gist Dec 9, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -127,4 +127,6 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    実行すると、下記のように通知が表示されます。

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/9c84d8ed3c0906ab4c3c67aa913aa013a43c8733/iPhone_LocalPush.png" width="621">
    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/9c84d8ed3c0906ab4c3c67aa913aa013a43c8733/iPhone_LocalPush.png" width="621">

    ## Android によるローカル通知
  22. pine revised this gist Dec 9, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -57,6 +57,8 @@
    - Android 5.0.2

    ## iOS によるローカル通知
    *サンプルコード: XXX*

    iOS でローカル通知を利用するには [User Notifications Framework](https://developer.apple.com/documentation/usernotifications) を利用します。これは iOS 10 以降で利用可能です。<br>
    iOS 9 では別の方法を使う必要がありますが、先述の通りこの記事では対象外なため解説しません。

  23. pine revised this gist Dec 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -125,4 +125,4 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    実行すると、下記のように通知が表示されます。

    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/d0ae031c0ccc87e23c575ef599e962d0a5b80222/iPhone_LocalPush.png">
    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/9c84d8ed3c0906ab4c3c67aa913aa013a43c8733/iPhone_LocalPush.png" width="621">
  24. pine revised this gist Dec 9, 2017. 1 changed file with 0 additions and 0 deletions.
    Binary file modified iPhone_LocalPush.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  25. pine revised this gist Dec 9, 2017. No changes.
  26. pine revised this gist Dec 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -125,4 +125,4 @@ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    実行すると、下記のように通知が表示されます。

    <img src="iPhone_LocalPush.png">
    <img src="https://gist.github.com/pine/8b7e58ff7fa259df17351d959c6118f7/raw/d0ae031c0ccc87e23c575ef599e962d0a5b80222/iPhone_LocalPush.png">
  27. pine revised this gist Dec 9, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -123,4 +123,6 @@ let request = UNNotificationRequest(identifier: "immediately", content: content,
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    ```

    実行すると、下記のように通知が表示されます。
    実行すると、下記のように通知が表示されます。

    <img src="iPhone_LocalPush.png">
  28. pine revised this gist Dec 9, 2017. 2 changed files with 0 additions and 0 deletions.
    Binary file added Welcome_to_Xcode.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
    Binary file added iPhone_LocalPush.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  29. pine revised this gist Dec 9, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -113,10 +113,12 @@ content.title = "れんちょんからのお知らせ"
    content.body = "にゃんぱすー"
    content.sound = UNNotificationSound.default()

    // 通知に画像を追加
    if let path = Bundle.main.path(forResource: "Renchon", ofType: "jpg") {
    content.attachments = [try! UNNotificationAttachment(identifier: "renchon", url: URL(fileURLWithPath: path), options: nil)]
    }

    // 直ぐに通知を表示
    let request = UNNotificationRequest(identifier: "immediately", content: content, trigger: nil)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    ```
  30. pine revised this gist Dec 9, 2017. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions AdventCalendar_KUT_20171217.md
    Original file line number Diff line number Diff line change
    @@ -106,7 +106,8 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
    ```

    ここまで終わると、下記のコードでプッシュ通知が発火できるようになります。
    ```

    ```swift
    let content = UNMutableNotificationContent()
    content.title = "れんちょんからのお知らせ"
    content.body = "にゃんぱすー"
    @@ -118,4 +119,6 @@ if let path = Bundle.main.path(forResource: "Renchon", ofType: "jpg") {

    let request = UNNotificationRequest(identifier: "immediately", content: content, trigger: nil)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    ```
    ```

    実行すると、下記のように通知が表示されます。