次の方法で共有


Xamarin.iOS での通知

重要

このセクションの情報は、iOS 9 以前に関連します。 iOS 10 以降については、ユーザー通知フレームワーク ガイドを参照してください。

iOS には、通知を受信したことをユーザーに示す 3 つの方法があります。

  • サウンドまたはバイブレーション - iOS では、ユーザーに通知するサウンドを再生できます。 サウンドが無効になっている場合は、デバイスをバイブレーションするように構成できます。
  • アラート - 通知に関する情報を含むダイアログを画面に表示できます。
  • バッジ - 通知が発行されると、アプリケーション アイコンに番号を表示 (バッジ) できます。

iOS には、ローカルとリモートの両方のすべての通知をユーザーに表示する "通知センター" も用意されています。 ユーザーは、画面の上部から下にスワイプして、これにアクセスできます。

通知センター

iOS でのローカル通知の作成

iOS を使用すると、ローカル通知の作成と処理が非常に簡単になります。 まず、iOS 8 では、通知を表示するためのユーザーのアクセス許可をアプリケーションによって要求する必要があります。 ローカル通知の送信を試す前に、次のコードをアプリに追加します。通常、AppDelegateFinishedLaunching メソッド内にあります。

var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
    UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
);
application.RegisterUserNotificationSettings(notificationSettings);

ローカル通知を送信する機能の確認

ローカル通知をスケジュールするには、UILocalNotification オブジェクトを作成し、FireDate を設定し、UIApplication.SharedApplication オブジェクトの ScheduleLocalNotification メソッドを使用してこれをスケジュールします。 次のコード スニペットは、1 分後に発生する通知をスケジュールし、メッセージと共にアラートを表示する方法を示しています。

UILocalNotification notification = new UILocalNotification();
notification.FireDate = NSDate.FromTimeIntervalSinceNow(15);
//notification.AlertTitle = "Alert Title"; // required for Apple Watch notifications
notification.AlertAction = "View Alert";
notification.AlertBody = "Your 15 second alert has fired!";
UIApplication.SharedApplication.ScheduleLocalNotification(notification);

次のスクリーンショットは、このアラートの外観を示しています。

アラートの例

ユーザーが通知を "許可しない" ことを選択した場合、何も表示されないことに注意してください。

数値を含むバッジをアプリケーション アイコンに適用する場合は、次の行コードに示すように設定できます。

notification.ApplicationIconBadgeNumber = 1;

アイコンを使用してサウンドを再生するには、次のコード スニペットに示すように、通知の SoundName プロパティを設定します。

notification.SoundName = UILocalNotification.DefaultSoundName;

通知のサウンドが 30 秒を超える場合、iOS では代わりに既定のサウンドを再生します。

重要

iOS シミュレーターには、デリゲート通知を 2 回起動するバグがあります。 この問題は、デバイスでアプリケーションを実行するときには発生しません。

通知の処理

iOS アプリケーションでは、リモートおよびローカルの通知をほぼ同じ方法で処理します。 アプリケーションが実行されている場合、AppDelegate クラスの ReceivedLocalNotification メソッドまたは ReceivedRemoteNotification メソッドが呼び出され、通知情報がパラメータとして渡されます。

アプリケーションでは、さまざまな方法で通知を処理できます。 たとえば、アプリケーションには、ユーザーにいくつかのイベントを通知する 1 つのアラートが表示されるだけの場合があります。 または、サーバーへのファイルの同期など、プロセスが完了したことを示すアラートをユーザーに表示するために通知を使用することもあります。

次のコードは、ローカル通知を処理し、アラートを表示し、バッジ番号をゼロにリセットする方法を示しています。

public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
    // show an alert
    UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert);
    okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

    Window.RootViewController.PresentViewController(okayAlertController, true, null);

    // reset our badge
    UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}

アプリケーションが実行されていない場合、iOS ではサウンドを再生したり、必要に応じてアイコン バッジを更新したりします。 ユーザーがアラートに関連付けられているアプリケーションを起動すると、アプリケーションが起動し、アプリ デリゲートの FinishedLaunching メソッドが呼び出され、通知情報が launchOptions パラメータを介して渡されます。 オプション辞書にキー UIApplication.LaunchOptionsLocalNotificationKey が含まれている場合は、アプリケーションがローカル通知から起動されたことが AppDelegate で認識されます。 次のコード スニペットは、このプロセスを示しています。

// check for a local notification
if (launchOptions.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
{
    var localNotification = launchOptions[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification;
    if (localNotification != null)
    {
        UIAlertController okayAlertController = UIAlertController.Create(localNotification.AlertAction, localNotification.AlertBody, UIAlertControllerStyle.Alert);
        okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

        Window.RootViewController.PresentViewController(okayAlertController, true, null);

        // reset our badge
        UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
    }
}

リモート通知の場合、launchOptions には、リモート通知ペイロードを含む関連する NSDictionary を持つ LaunchOptionsRemoteNotificationKey が指定されます。 通知ペイロードは、alertbadgesound の各キーを使用して抽出できます。 次のコード スニペットは、リモート通知を取得する方法を示しています。

NSDictionary remoteNotification = options[UIApplication.LaunchOptionsRemoteNotificationKey];
if(remoteNotification != null)
{
    string alert = remoteNotification["alert"];
}

まとめ

このセクションでは、Xamarin.iOS で通知を作成して発行する方法について説明しました。 AppDelegateReceivedLocalNotification メソッドまたは ReceivedRemoteNotification メソッドをオーバーライドすることで、アプリケーションでの通知の対応方法を示します。