質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

解決済

1回答

1435閲覧

【Xamarin.Forms】Firebase Cloud Messagingが動作しない【Android】

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2019/01/11 07:16

#開発環境
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を使用しているのですが、これはまずいのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

どこが動作しないのでしょうか?
同じように書いて試してみましたが、バックグランドでは、きちんと通知を受け取れました。
フォアグランドの場合は、チャンネルを設定していないので、確かにうまく動かないかもです。
MyFirebaseMessagingServiceSendNotificationを以下のように修正すればいいです。

C#

1var notificationBuilder = new NotificationCompat.Builder(this).SetSmallIcon(Resource.Mipmap.icon) 2 .SetContentTitle("アプリ") 3 .SetContentText(messageBody) 4 .SetAutoCancel(true) 5 .SetSound(defaultSoundUri) 6 .SetContentIntent(pendingIntent) 7 .SetChannelId(MainActivity.CHANNEL_ID);

ちなみに、Plugin.FirebasePushNotificationというライブラリを利用すれば、通知を受け取った時の処理を共通化できるので便利ですよ。参考までに。

投稿2019/01/11 10:57

f-miyu

総合スコア1625

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/01/11 11:13

回答ありがとうございます。 開発環境に書き忘れてしまいましたが当方Android 5.0.2 の端末で動作させております。 試したことに書いたようにプロジェクトをAndroid内のみに留める(言い方がわからず書いています、申し訳ありません)ようにするとしっかり通知を受け取れます。 これはフォアグラウンド、バックグラウンドに関わりません(ここも既におかしい気がします)。 コードに書いたようにLoadApplicationでAppを呼んでそちらの画面の処理を走らせると通知が動作しなくなります。 そのためXamarin.Formsで共通コードを使用するとサービスが動かなくなるのではないかと考えておりますが、f-miyu様の環境では動いているとなれば違いますね……。 ちなみに他のサービスも作成していますが、何故か自動起動しません(BroadcastReceiverを使用して呼び出すようにしているのですが引っかかりません)。 そちらはStartServiceで呼び出すことで動かしています。 全体的にサービス周りが怪しい気がするのですが今わかっているのはこれだけになります。
f-miyu

2019/01/11 11:42

Xamarin.Formsを使用したからといって、サービスが動かなくなるということはないと思います。 なんか他にも処理を色々やられているようですが、原因を特定するために、特に何も処理をしないシンプルな画面で通知の処理のみを入れて試してみてはいかがでしょうか?
退会済みユーザー

退会済みユーザー

2019/01/12 06:24

一度全て処理を行わないでページの表示のみを行ったら動作するようになりました。 その後処理を入れると問題なく動作しました。 ただしある程度新規動作を追加するとまた動作しなくなるようでした。 この場合も一旦全ての処理を行わないようにすることで動作するように出来ました。 とても不安定ですがなんとか動いているのでこのまま続けたいと思います。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問