メニュー

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

iOS でのプッシュ通知

プッシュ通知は、モバイルエクスペリエンスにおいて重要な位置を占めます。 ユーザーが使用するほぼすべてのAppの一部としてのプッシュ通知の存在がもはや当然のこととなっています。 iOS Programmable Chat SDKではプッシュ通知機能を提供すべく構築されています。

サービス・インスタンスに対してプッシュ通知を有効にする

IMPORTANT: The default enabled flag for new Service instances for all Push Notifications is false. This means that Push will be disabled until you explicitly enable it. To do so, please follow our Push Notification Configuration Guide.

Note: You will need to configure the sound setting value for each push notification type you want the sound payload parameter to present for, with required value. More information can be found in the above mentioned Push Notification Configuration Guide.

プッシュ・クレデンシャルの管理

Managing your push credentials will be necessary, as your device token is required for the Chat SDK to be able to send any notifications through APNS. Let's go through the process of managing your push credentials.

The AppDelegate class contains a series of application lifecycle methods. Many important events that occur like your app moving to the background or foreground have event listeners in this class.

When working with push notifications in your iOS application, it is quite likely you will find yourself needing to process push registrations or received events prior to the initialization of your Chat client. For this reason, we recommend you create a spot to store any registrations or push messages your application receives prior to the client being fully initialized. The best option here is to store these in a helper class to which your application delegate can obtain a reference. This way, your Chat client can process these values post-initialization if necessary or real-time otherwise. If you are doing a quick proof of concept, you could even define these on the application delegate itself but we recommend you refrain from doing this as storing state on the application delegate is not considered a best practice on iOS.

We will assume that you have defined the following properties in a way that makes them accessible to your application delegate method and Chat client initialization:

        
        
        
        

        Your users can choose to authorize notifications or not - if they have authorized notifications, you can register the application for remote notifications from Twilio. Typically, you would do this in AppDelegate.swift in the didFinishLaunchingWithOptions function.

              
              
              
              

              After successfully registering for remote notifications, the Apple Push Notification Service (APNS) will send back a unique device token that identifies this app installation on this device. The Twilio Chat Client will take that device token (as a Data object), and pass it to Twilio's servers to use to send push notifications to this device.

                    
                    
                    
                    

                    We print an error it it fails, but if it succeeds, we either update the Chat client directly or save the token for later use.

                    APN プッシュ用 Apple Developer クレデンシャル

                    Make sure you have created a push certificate on the Apple Developer Portal for your application first - for testing purposes, this should be a Sandbox certificate, but you will need a Production certificate for applications uploaded to the App Store.

                    We're going to need to export both a certificate and a private key from Keychain Access:

                    1. Mac で「Keychain Access」アプリケーションを起動します。
                    2. 左側のサイドバーで「My Certificates」カテゴリーを選択します。
                    3. Right-click the "Apple Development iOS Push Services" certificate for your application's bundle identifier
                    4. In the popup menu choose "Export..."
                    5. 「cred.p12」と名前を付けて、パスワード保護なし(パスワードに何も指定しない)で保存します。
                    6. Extract the certificate from "cred.p12" into a "cert.pem" file – run the following command in terminal:
                    openssl pkcs12 -in cred.p12 -nokeys -out cert.pem -nodes
                    
                    1. In the cert.pem file, strip anything outside of "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" boundaries, such as the "Bag Attributes"
                    2. Extract your private key from the "cred.p12" (PKCS#12) into the "key.pem" (PKCS#1) file using the following command in terminal
                    openssl pkcs12 -in cred.p12 -nocerts -out key.pem -nodes
                    

                    生成されるファイルには、"-----BEGIN RSA PRIVATE KEY-----" が含まれているはずです。ファイルに含まれているのが "-----BEGIN PRIVATE KEY-----" だった場合は、次のコマンドを実行します。

                    openssl rsa -in key.pem -out key.pem
                    

                    Strip anything outside of "-----BEGIN RSA PRIVATE KEY-----" and "-----END RSA PRIVATE KEY-----" boundaries and upload your credentials into the Twilio Platform through the Console.

                    To store your Credential, visit your Chat Push Credentials Console and click on the Create a Push Credential button. This console is located here:

                    Programmable Chat プッシュクレデンシャルコンソール

                    The Credential SID for your new Credential is in the detail page labeled 'Credential SID.'

                    You should also ensure you add your credential SID to the Access Token Chat grant for iOS endpoints request tokens. Each of the Twilio Helper Libraries makes provisions to add the push_credential_sid. Please see the relevant documentation for your preferred Helper Library for details.

                    var chatGrant = new ChatGrant({
                        serviceSid: ChatServiceSid,
                        pushCredentialSid: APNCredentialSid,
                    });
                    

                    これで、クライアントがデバイストークンにアクセスするために必要なことをすべて実行できました。

                    プッシュ通知の組み込み

                    You could keep your face buried in your phone all day waiting for your crush to send you a Chat message telling you that they're interested. Or you could turn on push notifications and go about your life, only getting notified once you receive their message. Sounds like a better option, right? Push notifications are supremely useful tools to keep users up to date with the status of their communication channels. Let's go through the process for integrating push notifications on iOS.

                    The AppDelegate class contains a series of application lifecycle methods. Many important events that occur like your app moving to the background or foreground have event listeners in this class. One of those is the applicationDidFinishLaunchingWithOptions method.

                          
                          
                          
                          

                          In this method, we're going to want to integrate push notifications for our app

                                
                                
                                
                                

                                The above code snippet asks the user's permission for notifications, and if granted, registers for remote (push) notifications. That's it! We're now registered for notifications.

                                通知の受信

                                Receiving notifications in our app lets us react to whatever event just occurred. It can trigger our app to update a view, change a status, or even send data to a server. Whenever the app receives a notification, the method didReceiveRemoteNotification is fired

                                      
                                      
                                      
                                      

                                      通知が初期化済みの場合は直接Chatクライアントに渡しますが、そうでない場合は後々処理するためにイベントを保存します。

                                      The userInfo parameter contains the data that the notification passes in from APNS. We can update our Chat client by passing it into the singleton via the receivedNotification method. The manager wraps the Chat client methods that process the notifications appropriately.

                                      クライアント起動時の統合

                                      Once your Chat client is up and available, you can provide the push token your application received:

                                            
                                            
                                            
                                            

                                            Update badge count

                                            To update badge count on an application icon, you should pass badge count from the Chat Client delegate to the application:

                                                  
                                                  
                                                  
                                                  
                                                  Rate this page:

                                                  ヘルプが必要ですか?

                                                  誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。