コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
//DependencyServiceから利用する
public interface INotificationService
{
//iOS用の登録
void Regist();
//通知する
void On(string title, string body);
//通知を解除する
void Off();
}
using Android.App;
using Android.Content;
using Android.Media;
using Xamarin.Forms;
using AppName.Services;
using AppName.Droid.Services;
[assembly: Dependency(typeof(NotificationService))]
public class NotificationService : INotificationService
{
// 通知のID
int id = 0;
public void Regist()
{
//iOS用なので、何もしない
}
public void On(string title, string body)
{
Context context = Forms.Context;
Intent intent = new Intent(context, typeof(MainActivity));
PendingIntent pendingIntent = PendingIntent.GetActivity(context, 0, intent, 0);
//デフォルトの通知音を取得
Android.Net.Uri uri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
Notification notification = new Notification.Builder(context)
.SetContentTitle(title)
.SetSmallIcon(Resource.Drawable.icon) //ターゲットフレームワークが7.0以降の場合は、動作OSが6.0以降にて透過アイコンを設定してください。
.SetColor(ActivityCompat.GetColor(_context, Resource.Color.notification_color)); //ターゲットフレームワークが7.0以降の場合は、動作OSが6.0以降ではColor.xmlに通知用のカラーを設定してください。
.SetContentText(body)
.SetOngoing(false) //常駐するかどうか
.SetContentIntent(pendingIntent)
.SetSound(uri) //通知音の設定
.Build();
NotificationManager manager = (NotificationManager)context.GetSystemService(Context.NotificationService);
manager.Notify(id, notification);
}
public void Off()
{
Context context = Forms.Context;
NotificationManager manager = (NotificationManager)context.GetSystemService(Context.NotificationService);
manager.Cancel(id);
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="splash_background">#CCCCCC</color>
</resources>
using System;
using Foundation;
using UIKit;
using Xamarin.Forms;
using AppName.Services;
using AppName.iOS.Services;
[assembly: Dependency(typeof(NotificationService))]
public class NotificationService : INotificationService
{
UILocalNotification _notification;
public void Regist()
{
// 許可をもらう通知タイプの種類を定義
UIUserNotificationType types = UIUserNotificationType.Badge | // アイコンバッチ
UIUserNotificationType.Sound | // サウンド
UIUserNotificationType.Alert; // テキスト
// UIUserNotificationSettingsの生成
UIUserNotificationSettings nSettings = UIUserNotificationSettings.GetSettingsForTypes(types, null);
// アプリケーションに登録
UIApplication.SharedApplication.RegisterUserNotificationSettings(nSettings);
}
public void On(string title, string body)
{
UIApplication.SharedApplication.InvokeOnMainThread(delegate
{
_notification = new UILocalNotification();
_notification.Init();
_notification.FireDate = NSDate.FromTimeIntervalSinceNow(10); //メッセージを通知する日時
_notification.TimeZone = NSTimeZone.DefaultTimeZone;
//_notification.RepeatInterval = NSCalendarUnit.Day; // 日々繰り返しする場合
_notification.AlertTitle = title;
_notification.AlertBody = body;
_notification.AlertAction = @"Open"; //ダイアログで表示されたときのボタンの文言
_notification.UserInfo = NSDictionary.FromObjectAndKey(new NSString("NotificationValue"), new NSString("NotificationKey"));
_notification.SoundName = UILocalNotification.DefaultSoundName;
// アイコン上に表示するバッジの数値
UIApplication.SharedApplication.ApplicationIconBadgeNumber += 1;
//通知を登録
UIApplication.SharedApplication.ScheduleLocalNotification(_notification);
});
}
public void Off()
{
UIApplication.SharedApplication.InvokeOnMainThread(delegate
{
//通知時に設定したUserInfoを元に通知情報をキャンセルする
if (_notification != null &&
(NSString)(_notification.UserInfo.ObjectForKey(new NSString("NotificationKey"))) == new NSString("NotificationValue"))
{
UIApplication.SharedApplication.CancelLocalNotification(_notification);
}
});
}
}
using AppName.Services;
using Xamarin.Forms;
public partial class TestPage : ContentPage
{
//PCLのXaml.csファイルのコンストラクタ等に記述します。
public TestPage()
{
//通知設定をiOSに登録
DependencyService.Get<INotificationService>().Regist();
}
//PCLのXaml.csファイルの任意の場所で記述します。
void OnNotificationClick(object sender, EventArgs e)
{
//通知を実行
DependencyService.Get<INotificationService>().On(”タイトルテスト”, ”本文テスト”);
}
}
public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
//画面に通知を表示するダイアログ
var alert = new UIAlertView();
alert.Title = notification.AlertTitle;
alert.Message = notification.AlertBody;
alert.AddButton(notification.AlertAction);
alert.Show();
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。