アプリが(フォアグラウンドとバックグラウンドで)実行されているときに、通知を受け取り、AndroidのNotificationManager
を使用して通知を表示できます。しかし、アプリを通知から外した後、アプリが通知を受け取ると、Unfortunately, MyApp has stopped working
というメッセージが表示されます。アプリを閉じるとGCM通知がクラッシュする
私は既にBaidu Notificationsを設定しているため、アプリをスワイプした後に通知を受け取ることができることを知っています。しかし、GCMを使用すると、アプリがクラッシュするだけです。ここでは、コードに問題がある:
[BroadcastReceiver(Name = "com.my.pkg.MyGcmReceiver", Exported = true, Permission = "com.google.android.c2dm.permission.SEND")]
[IntentFilter(new string[] { "com.google.android.c2dm.intent.RECEIVE" }, Categories = new string[] { "com.my.pkg" })]
public class MyGcmReceiver : GcmReceiver
{
public override void OnReceive(Context context, Intent intent)
{
base.OnReceive(context, intent);
System.Diagnostics.Debug.WriteLine("MyGcmReceiver - OnReceive called &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
CreateNotification();
}
private void CreateNotification()
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(Xamarin.Forms.Forms.Context);
builder.SetAutoCancel(true);
builder.SetDefaults(NotificationCompat.DefaultAll);
builder.SetSmallIcon(Resource.Drawable.icon);
builder.SetContentTitle("HI");
builder.SetContentText("MSG FROM GCM");
builder.SetPriority((int)NotificationPriority.Max);
NotificationManager manager = (NotificationManager)Xamarin.Forms.Forms.Context.GetSystemService(Context.NotificationService);
manager.Notify(0, builder.Build());
}
}
OnReceive
方法で行CreateNotification();
をコメントアウトすることで、アプリがクラッシュしませんが、もちろんそれは、通知を作成し、表示されません。
Google Cloud Messagingを使用していてFirebase Could Messagingを使用していない理由は何ですか? –
@MadhavShenoy私は同じことを書いていました。 –
:D FCMでは通知を処理するコードを書く必要はありません。 –