#開発環境
Visual Studio 2017 Community 15.9.4
Xamarin.Forms 3.4.0.1009999
Xamarin.GooglePlayServices.Base 60.1142.1
Xamarin.GooglePlayServices.Tasks 60.1142.1
Xamarin.Firebase.Messaging 60.1142.1
Xamarin.Forms.GoogleMaps 3.0.4
Xam.Plugin.Geolocator 4.5.0.6
#やりたいこと
Firebase Cloud Messagingを用い、Android端末に通知を出したい。
#コード
MainActivity.cs
C#
1namespace App.Droid 2{ 3 public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 4 { 5 static readonly string TAG = "MainActivity"; 6 7 internal static readonly string CHANNEL_ID = "my_notification_channel"; 8 internal static readonly int NOTIFICATION_ID = 100; 9 10 protected override void OnCreate(Bundle savedInstanceState) 11 { 12 TabLayoutResource = Resource.Layout.Tabbar; 13 ToolbarResource = Resource.Layout.Toolbar; 14 15 base.OnCreate(savedInstanceState); 16 IsPlayServicesAvailable(); 17 18 if (Intent.Extras != null) 19 { 20 foreach (var key in Intent.Extras.KeySet()) 21 { 22 var value = Intent.Extras.GetString(key); 23 Console.WriteLine("Key: " + key + " Value: " + value); 24 } 25 } 26 27 CreateNotificationChannel(); 28 29 global::Xamarin.Forms.Forms.Init(this, savedInstanceState); 30 31 Xamarin.FormsGoogleMaps.Init(this, savedInstanceState); 32 Xamarin.FormsGoogleMapsBindings.Init(); 33 Firebase.FirebaseApp.InitializeApp(this); 34 35 LoadApplication(new App()); 36 } 37 38 public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults) 39 { 40 PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults); 41 } 42 43 public bool IsPlayServicesAvailable() 44 { 45 int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this); 46 if (resultCode != ConnectionResult.Success) 47 { 48 if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode)) 49 { 50 Console.WriteLine(GoogleApiAvailability.Instance.GetErrorString(resultCode)); 51 } 52 else 53 { 54 Console.WriteLine("このデバイスはサポートされていません。"); 55 Finish(); 56 } 57 return false; 58 } 59 else 60 { 61 Console.WriteLine("Google Play Serviceが使用できます。"); 62 return true; 63 } 64 } 65 66 void CreateNotificationChannel() 67 { 68 if (Build.VERSION.SdkInt < BuildVersionCodes.O) 69 { 70 return; 71 } 72 73 var channel = new NotificationChannel(CHANNEL_ID, 74 "FCM Notification", 75 NotificationImportance.Default) 76 { 77 Description = "Firebase Cloud Messages appear in this channel" 78 }; 79 80 var notificationManager = (NotificationManager)GetSystemService(Android.Content.Context.NotificationService); 81 notificationManager.CreateNotificationChannel(channel); 82 } 83 } 84}
MyFirebaseIIdService.cs
C#
1namespace App.Droid 2{ 3 [Service] 4 [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })] 5 public class MyFirebaseIIdService : FirebaseInstanceIdService 6 { 7 public override void OnTokenRefresh() 8 { 9 var refreshedToken = FirebaseInstanceId.Instance.Token; 10 SendRegistrationToServer(refreshedToken); 11 } 12 13 async void SendRegistrationToServer(string token) 14 { 15 // サーバーへの送信処理 16 } 17 } 18}
MyFirebaseMessagingService.cs
C#
1namespace App.Droid 2{ 3 [Service] 4 [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] 5 class MyFirebaseMessagingService : FirebaseMessagingService 6 { 7 public override void OnMessageReceived(RemoteMessage message) 8 { 9 SendNotification(message.GetNotification().Body); 10 } 11 12 void SendNotification(string messageBody) 13 { 14 var intent = new Intent(this, typeof(MainActivity)); 15 intent.AddFlags(ActivityFlags.ClearTop); 16 var pendingIntent = PendingIntent.GetActivity(this, 999 /* Request code */, intent, PendingIntentFlags.OneShot); 17 18 var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification); 19 var notificationBuilder = new NotificationCompat.Builder(this).SetSmallIcon(Resource.Mipmap.icon) 20 .SetContentTitle("アプリ") 21 .SetContentText(messageBody) 22 .SetAutoCancel(true) 23 .SetSound(defaultSoundUri) 24 .SetContentIntent(pendingIntent); 25 26 var notificationManager = NotificationManager.FromContext(this); 27 28 notificationManager.Notify(999 /* ID of notification */, notificationBuilder.Build()); 29 } 30 } 31}
AndroidManifesto.xml
XML
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.App" android:installLocation="internalOnly"> 3 <uses-sdk android:minSdkVersion="20" android:targetSdkVersion="27" /> 4 <uses-permission android:name="android.permission.INTERNET" /> 5 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 6 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 7 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 8 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 9 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 10 <!-- OpenGL ESの設定 --> 11 <uses-feature android:glEsVersion="0x00020000" android:required="true" /> 12 <application android:label="App.Android"> 13 <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" /> 14 <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> 15 <intent-filter> 16 <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 17 <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 18 <category android:name="${applicationId}" /> 19 </intent-filter> 20 </receiver> 21 </application> 22</manifest>
#試したこと
Android側のlayoutに以下を追加し、MainActivityのコードを変更してみました。
Main.axml
axml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:padding="10dp"> 7 <TextView 8 android:text=" " 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:id="@+id/msgText" 12 android:textAppearance="?android:attr/textAppearanceMedium" 13 android:padding="10dp" /> 14 <Button 15 android:id="@+id/logTokenButton" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:layout_gravity="center_horizontal" 19 android:text="Log Token" /> 20</LinearLayout>
MainActivity.cs
C#
1namespace App.Droid 2{ 3 [Activity(Label = "App", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)] 4 public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 5 { 6 static readonly string TAG = "MainActivity"; 7 8 internal static readonly string CHANNEL_ID = "my_notification_channel"; 9 internal static readonly int NOTIFICATION_ID = 100; 10 11 TextView msgText; 12 13 protected override void OnCreate(Bundle savedInstanceState) 14 { 15 TabLayoutResource = Resource.Layout.Tabbar; 16 ToolbarResource = Resource.Layout.Toolbar; 17 18 base.OnCreate(savedInstanceState); 19 20 SetContentView(Resource.Layout.Main); 21 msgText = FindViewById<TextView>(Resource.Id.msgText); 22 23 IsPlayServicesAvailable(); 24 25 if (Intent.Extras != null) 26 { 27 foreach (var key in Intent.Extras.KeySet()) 28 { 29 var value = Intent.Extras.GetString(key); 30 Console.WriteLine("Key: " + key + " Value: " + value); 31 } 32 } 33 34 CreateNotificationChannel(); 35 36 Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState); 37 38 global::Xamarin.Forms.Forms.Init(this, savedInstanceState); 39 40 Xamarin.FormsGoogleMaps.Init(this, savedInstanceState); 41 Xamarin.FormsGoogleMapsBindings.Init(); 42 Firebase.FirebaseApp.InitializeApp(this); 43 44 var logTokenButton = FindViewById<Button>(Resource.Id.logTokenButton); 45 logTokenButton.Click += delegate 46 { 47 Android.Util.Log.Debug(TAG, "InstanceID token: " + FirebaseInstanceId.Instance.Token); 48 }; 49 } 50 } 51}
このように変更すると動作することが確認できました。
#知りたいこと
Firebase Cloud MessagingはAndroidのコードのみで書かないと動作しないのでしょうか?
iOSと一部共有したいので、Xamarin.FormsのAndroid、iOSを使用しているのですが、これはまずいのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/01/11 11:13
2019/01/11 11:42
退会済みユーザー
2019/01/12 06:24