티스토리 뷰

iOS도 안드로이드 처럼 푸시 인증 키를 사용하면 1년에 한번씩 푸시 인증서 교체를 안해도 된다.


푸시 인증 키를 생성하는 방법은 아래와 같다.


Keys에서 오른쪽 상단 +를 눌러 아래와 같이 작성한다.



다음 화면은 아래와 같다.


여기서 Confirm을 누르면 p8 파일을 다운로드 받는 화면이 나오고 여기서 밖에 다운로드가 안되니 주의하자.


인증 키로 푸시를 보내기 위해선 다운받은 p8파일, Key ID, Team ID가 필요하다.

아래 주소에서 확인 가능하다.

Key ID - https://developer.apple.com/account/#/membership/

Team ID - https://developer.apple.com/account/#/membership/


이제 푸시를 보내기 위해서 아래와 같이 준비하자.

(참고: https://eladnava.com/send-push-notifications-to-ios-devices-using-xcode-8-and-swift-3/)


1. Node JS 설치

mkdir apns  

cd apns  

npm init --yes  

npm install apn --save 


2. app.js 작성

- key : 다운로드 받은 파일 (위에 만든 apns 폴더에 같이 두는게 편리하다)

- keyId, teamId 작성

- deviceToken 작성

- notification.topic 에 Bundle ID를 넣어주면 된다.

var apn = require('apn');


// Set up apn with the APNs Auth Key

var apnProvider = new apn.Provider({  

     token: {

        key: 'AuthKey.p8', // Path to the key p8 file

        keyId: 'ABCDE12345', // The Key ID of the p8 file (available at https://developer.apple.com/account/ios/certificate/key)

        teamId: 'ABCDE12345', // The Team ID of your Apple Developer Account (available at https://developer.apple.com/account/#/membership/)

    },

    production: false // Set to true if sending a notification to a production iOS app

});


// Enter the device token from the Xcode console

var deviceToken = '5311839E985FA01B56E7AD74444C0157F7F71A2745D0FB50DED665E0E882';


// Prepare a new notification

var notification = new apn.Notification();


// Specify your iOS app's Bundle ID (accessible within the project editor)

notification.topic = 'my.bundle.id';


// Set expiration to 1 hour from now (in case device is offline)

notification.expiry = Math.floor(Date.now() / 1000) + 3600;


// Set app badge indicator

notification.badge = 3;


// Play ping.aiff sound when the notification is received

notification.sound = 'ping.aiff';


// Display the following message (the actual notification text, supports emoji)

notification.alert = 'Hello World \u270C';


// Send any extra payload data with the notification which will be accessible to your app in didReceiveRemoteNotification

notification.payload = {id: 123};


// Actually send the notification

apnProvider.send(notification, deviceToken).then(function(result) {  

    // Check the result for any failed devices

    console.log(result);

}); 


3. 푸시 보내기

node app.js 



앱 업데이트 없이 사용가능하고 한번 만들어 놓으면 더 이상 신경 쓸일이 없다.