代码之家  ›  专栏  ›  技术社区  ›  Dumber_Texan2

无法在Xamarin.Forms Android应用程序关闭、停止或未运行时显示FCM远程通知

  •  0
  • Dumber_Texan2  · 技术社区  · 6 年前

    我使用这些说明将基于Azure通知中心FCM的远程通知添加到我的Xamarin.Forms Android应用程序中。

    https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-push

    我的测试设备正在运行API级别22,因此我使用以下方法在设备上生成通知。

    Android.Support.V4.App.NotificationCompat.Builder(这个)

    对于API级别26+,我将使用以下方法和一个通道在设备上构建通知。

    var builder=new Android.App.Notification.builder(这个)

    我想我必须使用一个广播接收器来实现这一点,但我真的不知道,在阅读了这么多关于这个主题的评论。我的应用程序是使用API27编译的,目标是API27。

    方法1

    我正在尝试创建一个广播接收器,当通知到达时,它将使用显式意图启动MyService。不幸的是,这在我的API级别22测试设备上不起作用。

    //[BroadcastReceiver]
    //[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
    [BroadcastReceiver(Enabled = true, Exported = true)]
        [IntentFilter(new[] { "com.xamarin.example.TEST" })]
        public class MyBroadcastReceiver : BroadcastReceiver
        {
    
            public override void OnReceive(Context context, Intent intent)
            {
                string message = intent.GetStringExtra("message");
                string title = intent.GetStringExtra("title");
                string id = intent.GetStringExtra("id");
    
                //Explicit Intent to launch MyService
                Intent intent2 = new Intent(Application.Context, typeof(MyService));
                intent2.PutExtra("message", message);
                intent2.PutExtra("title", title);
                intent2.PutExtra("id", id);
    
                Application.Context.StartService(intent2);
            }
    
         }
    
    
    // Service is exported and given a name so other applications can use it
        [Service(Exported = true, Name = "com.mycompany.myapp.MyService")]
        // Intent filter only needed for Implicit Intents
        //[IntentFilter(new string[] { "com.xamarin.example.TEST" })]
        public class MyService : Service
        {
    
            public static string PRIMARY_NOTIF_CHANNEL = "default";
    
            public override IBinder OnBind(Intent intent)
            {
                return null;
            }
    
            [return: GeneratedEnum]
            public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
            {
                var pm = PowerManager.FromContext(this);
                var wakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "Notification");
                wakeLock.Acquire();
    
                string message = intent.GetStringExtra("message");
                string title = intent.GetStringExtra("title");
                string id = intent.GetStringExtra("id");
    
                var intent2 = new Intent(this, typeof(MainActivity));
                intent2.PutExtra("id", id);
                intent2.AddFlags(ActivityFlags.ClearTop);
    
                var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
    
                NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
    
                Android.Support.V4.App.NotificationCompat.Builder builder = new Android.Support.V4.App.NotificationCompat.Builder(this)
                       .SetAutoCancel(true)
                       .SetContentIntent(pendingIntent)
                       .SetContentTitle(title)
                       .SetContentText(message)
                       .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
                       .SetVibrate(new long[1])
                       //1 is Hi
                       .SetPriority(1)
                       .SetLargeIcon(BitmapFactory.DecodeResource(Resources, SalesFlash.Droid.Resource.Drawable.Icon_lg))
                       .SetSmallIcon(MyApp.Droid.Resource.Drawable.icon)
                       .SetChannelId(PRIMARY_NOTIF_CHANNEL);
    
                notificationManager = NotificationManager.FromContext(this);
                notificationManager.Notify(0, builder.Build());
    
                wakeLock.Release();
    
                //return StartCommandResult.Sticky;
    
                return base.OnStartCommand(intent, flags, startId);
            }
    
            public override void OnDestroy()
            {
                base.OnDestroy();
            }
        }
    

    根据这篇文章,广播接收器不会为FCM通知工作。 https://stackoverflow.com/a/44287962/5360237

    这篇文章似乎显示一个广播接收器正在接受通知。 https://stackoverflow.com/a/45616014/5360237

    任何帮助都非常感谢。提前谢谢!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Dumber_Texan2    6 年前

    这是我读过的解释这个问题的最好的文章。它还为受影响的设备提供了手动解决方案。 https://medium.freecodecamp.org/why-your-push-notifications-never-see-the-light-of-day-3fa297520793

    注意:消息是使用数据负载传递到我的Android应用程序的。即使我在前台和后台收到通知,我还是继续向AndroidManifest添加了以下权限。

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  
    

    此权限用于自动启动。我还使用Azure应用中心向我的测试设备分发了一个签名的APK。希望这能帮助别人。

    更新:我删除了 RECEIVE_BOOT_COMPLETE 权限并进行了调试生成。我通过将应用程序从屏幕上划掉而关闭了调试(连接到Visual Studio)。然后我发送了一个通知,但它没有显示出来。然后我重新打开了应用程序(Visual Studio没有调试),并通过从屏幕上滑动关闭了它,发送了一个通知,它就工作了。所以,这和 接收启动完成