개요
이 자습서에서는 레코드가 삽입될 때마다 푸시 알림이 디바이스로 전송되도록 iOS 빠른 시작 프로젝트에 푸시 알림을 추가합니다.
다운로드한 빠른 시작 서버 프로젝트를 사용하지 않는 경우 푸시 알림 확장 패키지가 필요합니다. 자세한 내용은 Azure Mobile Apps용 .NET 백 엔드 서버 SDK 작업 가이드를 참조하세요.
iOS 시뮬레이터는 푸시 알림지원하지 않습니다. 물리적 iOS 디바이스 및 Apple Developer Program 멤버 자격필요합니다.
알림 허브 구성
Azure App Service의 Mobile Apps 기능은 Azure Notification Hubs 사용하여 푸시를 보내므로 모바일 앱에 대한 알림 허브를 구성합니다.
Azure 포털에서 App Services로 이동하여 앱 백 엔드를 선택합니다. 설정에서, 푸시를선택합니다.
앱에 알림 허브 리소스를 추가하려면 연결선택합니다. 허브를 만들거나 기존 허브에 연결할 수 있습니다.
이제 Mobile Apps 백 엔드 프로젝트에 알림 허브를 연결했습니다. 나중에 디바이스에 푸시할 PNS(플랫폼 알림 시스템)에 연결하도록 이 알림 허브를 구성합니다.
푸시 알림에 대한 앱 등록
- 앱을(를) 위한 앱 ID를 등록하세요. 명시적 앱 ID(와일드카드 앱 ID 아님)를 만들고 번들 IDXcode 빠른 시작 프로젝트에 있는 정확한 번들 ID를 사용합니다. 푸시 알림 옵션을 선택하는 것도 중요합니다.
- 그런 다음 푸시 알림 구성을 준비하려면 "개발" 또는 "배포" SSL 인증서를 만듭니다.
푸시 알림을 보내도록 Azure 구성
- Mac에서 Keychain Access시작합니다. 왼쪽 탐색 모음의 범주아래에서 내 인증서를 엽니다. 이전 섹션에서 다운로드한 SSL 인증서를 찾은 다음 해당 내용을 공개합니다. 인증서만 선택합니다(프라이빗 키를 선택하지 않음). 그리고 그것을 내보내세요.
- Azure Portal모든>App Services찾아보기를 선택합니다. 그런 다음 Mobile Apps 백 엔드를 선택합니다.
- 설정에서 App Service 푸시를 선택합니다. 그런 다음 알림 허브 이름을 선택합니다.
- Apple Push Notification Services >인증서업로드로 이동합니다. .p12 파일을 업로드하여 올바른 모드 선택합니다(이전의 클라이언트 SSL 인증서가 프로덕션 또는 샌드박스인지 여부에 따라 다름). 변경 내용을 저장합니다.
이제 서비스가 iOS에서 푸시 알림으로 작동하도록 구성되었습니다.
푸시 알림을 보내도록 백 엔드 업데이트
.NET 백 엔드(C#):
Visual Studio에서 서버 프로젝트를 마우스 오른쪽 단추로 클릭하고 NuGet 패키지관리를 클릭하고
Microsoft.Azure.NotificationHubs검색한 다음 설치클릭합니다. 그러면 백 엔드에서 알림을 보내기 위한 Notification Hubs 라이브러리가 설치됩니다.백 엔드의 Visual Studio 프로젝트에서 컨트롤러>TodoItemController.cs엽니다. 파일 맨 위에 다음
using문을 추가합니다.using Microsoft.Azure.Mobile.Server.Config; using Microsoft.Azure.NotificationHubs;PostTodoItem메서드를 다음 코드로 바꿉니다.public async Task<IHttpActionResult> PostTodoItem(TodoItem item) { TodoItem current = await InsertAsync(item); // Get the settings for the server project. HttpConfiguration config = this.Configuration; MobileAppSettingsDictionary settings = this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings(); // Get the Notification Hubs credentials for the Mobile App. string notificationHubName = settings.NotificationHubName; string notificationHubConnection = settings .Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString; // Create a new Notification Hub client. NotificationHubClient hub = NotificationHubClient .CreateClientFromConnectionString(notificationHubConnection, notificationHubName); // iOS payload var appleNotificationPayload = "{\"aps\":{\"alert\":\"" + item.Text + "\"}}"; try { // Send the push notification and log the results. var result = await hub.SendAppleNativeNotificationAsync(appleNotificationPayload); // Write the success result to the logs. config.Services.GetTraceWriter().Info(result.State.ToString()); } catch (System.Exception ex) { // Write the failure result to the logs. config.Services.GetTraceWriter() .Error(ex.Message, null, "Push.SendAsync Error"); } return CreatedAtRoute("Tables", new { id = current.Id }, current); }서버 프로젝트를 다시 게시합니다.
Node.js 백엔드:
백 엔드 프로젝트를 설정합니다.
todoitem.js 테이블 스크립트를 다음 코드로 바꿉다.
var azureMobileApps = require('azure-mobile-apps'), promises = require('azure-mobile-apps/src/utilities/promises'), logger = require('azure-mobile-apps/src/logger'); var table = azureMobileApps.table(); // When adding record, send a push notification via APNS table.insert(function (context) { // For details of the Notification Hubs JavaScript SDK, // see https://aka.ms/nodejshubs logger.info('Running TodoItem.insert'); // Create a payload that contains the new item Text. var payload = "{\"aps\":{\"alert\":\"" + context.item.text + "\"}}"; // Execute the insert; Push as a post-execute action when results are returned as a Promise. return context.execute() .then(function (results) { // Only do the push if configured if (context.push) { context.push.apns.send(null, payload, function (error) { if (error) { logger.error('Error while sending push notification: ', error); } else { logger.info('Push notification sent successfully!'); } }); } return results; }) .catch(function (error) { logger.error('Error while running context.execute: ', error); }); }); module.exports = table;로컬 컴퓨터에서 파일을 편집할 때 서버 프로젝트를 다시 게시합니다.
앱에 푸시 알림 추가
Objective-C:
QSAppDelegate.m에서 iOS SDK와 QSTodoService.h를 임포트하십시오.
#import <MicrosoftAzureMobile/MicrosoftAzureMobile.h> #import "QSTodoService.h"didFinishLaunchingWithOptions의 에서,return YES;바로 앞에 다음 줄을 삽입합니다.UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; [[UIApplication sharedApplication] registerForRemoteNotifications];QSAppDelegate.m에 다음 처리기 메서드를 추가하세요. 이제 푸시 알림을 지원하도록 앱이 업데이트되었습니다.
// Registration with APNs is successful - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { QSTodoService *todoService = [QSTodoService defaultService]; MSClient *client = todoService.client; [client.push registerDeviceToken:deviceToken completion:^(NSError *error) { if (error != nil) { NSLog(@"Error registering for notifications: %@", error); } }]; } // Handle any failure to register - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError: (NSError *)error { NSLog(@"Failed to register for remote notifications: %@", error); } // Use userInfo in the payload to display an alert. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"%@", userInfo); NSDictionary *apsPayload = userInfo[@"aps"]; NSString *alertString = apsPayload[@"alert"]; // Create alert with notification content. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Notification" message:alertString preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"Cancel"); }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"OK"); }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; // Get current view controller. UIViewController *currentViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; while (currentViewController.presentedViewController) { currentViewController = currentViewController.presentedViewController; } // Display alert. [currentViewController presentViewController:alertController animated:YES completion:nil]; }
Swift:
다음 내용과 함께 ClientManager.swift 파일을 추가합니다. %AppUrl% Azure Mobile App 백 엔드의 URL로 바꿉니다.
class ClientManager { static let sharedClient = MSClient(applicationURLString: "%AppUrl%") }toDoTableViewController.swift
let client초기화하는MSClient줄을 다음 줄로 바꿉니다.let client = ClientManager.sharedClientAppDelegate.swift에서
func application본문을 다음과 같이 바꿉니다.func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { application.registerUserNotificationSettings( UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)) application.registerForRemoteNotifications() return true }AppDelegate.swift파일에 다음의 처리기 메서드를 추가합니다. 이제 푸시 알림을 지원하도록 앱이 업데이트되었습니다.
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { ClientManager.sharedClient.push?.registerDeviceToken(deviceToken) { error in print("Error registering for notifications: ", error?.description) } } func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { print("Failed to register for remote notifications: ", error.description) } func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject: AnyObject]) { print(userInfo) let apsNotification = userInfo["aps"] as? NSDictionary let apsString = apsNotification?["alert"] as? String let alert = UIAlertController(title: "Alert", message: apsString, preferredStyle: .Alert) let okAction = UIAlertAction(title: "OK", style: .Default) { _ in print("OK") } let cancelAction = UIAlertAction(title: "Cancel", style: .Default) { _ in print("Cancel") } alert.addAction(okAction) alert.addAction(cancelAction) var currentViewController = self.window?.rootViewController while currentViewController?.presentedViewController != nil { currentViewController = currentViewController?.presentedViewController } currentViewController?.presentViewController(alert, animated: true) {} }
푸시 알림 테스트
- Xcode에서 실행하고 iOS 디바이스에서 앱을 시작합니다(시뮬레이터에서는 푸시가 작동하지 않음). 확인 클릭하여 푸시 알림을 수락합니다. 이 요청은 앱이 처음 실행되면 발생합니다.
- 앱에서 새 항목을 추가하고 +클릭합니다.
- 알림이 수신되었는지 확인한 다음 확인 클릭하여 알림을 해제합니다. 이제 이 자습서를 성공적으로 완료했습니다.
더
- 템플릿을 사용하면 플랫폼 간 푸시 및 지역화된 푸시를 유연하게 보낼 수 있습니다. Azure Mobile Apps용 iOS 클라이언트 라이브러리를 사용하는 템플릿을 등록하는 방법을 보여 줍니다.