使用通知通道在后台向大于26的API发送通知

时间:2018-08-03 20:54:04

标签: android xamarin push-notification

我需要一些帮助。

我的应用程序可以在API 26以下的背景中发送推送通知。这些通知显示在Android的通知抽屉中。它们在前台时可在ANY API上工作,但在API 26以上时,通知不会在后台发送。

我了解我必须使用API​​ 26及更高版本的通知渠道,并且正在按照教程进行操作,但是由于出现此问题,我在此时遇到了编译错误 找不到 noti_chan_urgent。

这也是做API> 26的后台通知发送的好地方吗?

/

/ this service is used if the app is in the foreground and a message is received
        [Service]
        [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
        public class MyFirebaseMessagingService : FirebaseMessagingService
        {
            public const string URGENT_CHANNEL = "com.wj.sa.urgent";

            public override void OnMessageReceived(RemoteMessage message)
            {
                base.OnMessageReceived(message);

                Console.WriteLine("Received: " + message);


                //get current build
                var currentBuild = Build.VERSION.Sdk;

                // check if API > 26 and do the background notification send if true
                if (Int32.Parse(currentBuild) >= 26)
                {
                    string chanName = GetString(Resource.String.noti_chan_urgent);     //error happens here
                    var importance = NotificationImportance.High;
                    NotificationChannel chan =
                       new NotificationChannel(URGENT_CHANNEL, chanName, importance);

                    // need to continue here  --- incomplete ---
                }

                try
                {
                    var msg = message.GetNotification().Body;

                    //pass message to xamarin forms, the picked up by main page and update the label if app is active
                    MessagingCenter.Send<object, string>(this, ShakeAlarmRR1.App.NotificationReceivedKey, msg);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error extracting message: " + ex);
                }
            }
        }

0 个答案:

没有答案