Xamarin Forms-Android-FCM-当应用程序处于前台,后台且滑动关闭状态时,会收到抬头通知

时间:2020-07-08 21:02:42

标签: android xamarin.forms push-notification firebase-cloud-messaging

请给我一些有关Android推送通知的帮助。对于上下文,我已经阅读了数十篇有关如何为Android实现推送通知的堆栈溢出文章。我见过的所有帖子仅涵盖了部分问题。当应用程序处于前台或后台时,我已经能够成功接收推送通知并显示抬头通知,但是当应用程序在关闭状态下滑动时,它不起作用。为了在关闭应用程序时将通知显示在状态栏上,您需要在有效负载中使用“通知”对象,而不是“数据”对象。

我要达到的目的是-当应用程序处于以下任意一种状态(前景,背景和滑动关闭)时,我需要状态栏通知,声音和提示通知。

要实现此目的,代码,有效负载json和清单配置的秘诀是什么?我在手机上使用的任何主要应用程序-eBay,etsy,amazon和我下载的大多数应用程序都可以完成所有这些操作,因此无论fcm指南中的内容如何,​​都必须可行。

这是FireBaseMessagingService中的onMessageReceived方法:

  public override void OnMessageReceived(RemoteMessage message)
  {
    try
    {
        base.OnMessageReceived(message);
        string messageBody = string.Empty;
        string messageTitle = string.Empty;

        if (message.GetNotification() != null)
        {
            messageTitle = message.GetNotification().Title;
            messageBody = message.GetNotification().Body;
        }
        else
        {
            if (message.Data.Values.Count == 1)
            {
                messageTitle = "Alerts Occurred";
                messageBody = message.Data.Values.First();
            }
            else if (message.Data.Values.Count == 2)
            {
                messageTitle = message.Data.Values.ToList()[0];
                messageBody = message.Data.Values.ToList()[1];
            }
        }

        SendLocalNotification(messageTitle, messageBody);

    }
    catch (Exception e)
    {
        NotificationHubHelper.LogInfo($"Error receiving message on device: {e.Message}");
    }
  }

   private void SendLocalNotification(string title, string body)
  {
    try
    {
        _notificationBadgeCount++;
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        intent.PutExtra("message", body);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        int notificationIconId = 
Resources.GetIdentifier(NotificationHubHelper.AndroidNotificationIconImage, "drawable", 
AppInfo.PackageName);
        NotificationHubHelper.LogInfo($"Found notification icon with id: {notificationIconId}");
        var notificationBuilder = new NotificationCompat.Builder(this, 
NotificationHubHelper.NotificationChannelName)
            .SetContentTitle(title)
            .SetSmallIcon(notificationIconId) //.SetSmallIcon(ApplicationInfo.Icon)
            .SetContentText(body)
            .SetAutoCancel(true)
            .SetShowWhen(false)
            .SetContentIntent(pendingIntent)
            .SetPriority((int)NotificationPriority.Max);

        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            notificationBuilder.SetChannelId(NotificationHubHelper.NotificationChannelName);
        }

        var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0, notificationBuilder.Build());
        CrossBadge.Current.SetBadge(_notificationBadgeCount);
    }
    catch (Exception e)
    {
        NotificationHubHelper.LogInfo($"Error sending local notification on device: {e.Message}");
    }
 }

这是我们的清单:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" 
    android:versionName="1.0.9" package="HIDDEN_FROM_POST" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="29" />
    <application android:allowBackup="false" android:label="HIDDEN_FROM_POST" 
    android:icon="@drawable/HIDDEN_FROM_POST">
        <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="HIDDEN_FROM_POST" />
        <meta-data android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" />
        <uses-library android:name="org.apache.http.legacy" android:required="false" />
        <provider android:name="android.support.v4.content.FileProvider" 
    android:authorities="${applicationId}.fileprovider" android:exported="false" 
    android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" 
    android:resource="@xml/file_paths"></meta-data>
        </provider>
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" 
    android:exported="false" />
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" 
    android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="${applicationId}" />
            </intent-filter>
        </receiver>
    <meta-data
      android:name="com.google.firebase.messaging.default_notification_icon"
      android:resource="@drawable/HIDDEN_FROM_POST" />
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
  <uses-permission android:name="com.anddoes.launcher.permission.UPDATE_COUNT"/>
  <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
  <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS"/>
  <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
  <uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS" />
  <uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT" /> 
  <uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
  <uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />
  <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />
  <uses-permission android:name="com.sonymobile.home.permission.PROVIDER_INSERT_BADGE" />
</manifest>

最后,这是我们尝试过的最新有效负载:

   {
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data":{
        "message":"this is a message",
        "title":"title"
    }
}

通过上述操作,我们将在滑动关闭应用程序时收到状态栏通知,在应用程序处于后台时会显示状态栏通知,而在应用程序处于前台时会显示带有抬头通知的状态栏通知。 >

我们如何在所有情况下获得抬头通知?

Len

2 个答案:

答案 0 :(得分:0)

它与普通通知基本相同。在Android 8.0及更高版本中,您只能通过将通知渠道的重要性的重要性级别设置为IMPORTANCE_HIGH来暂停通知。

NotificationChannel notificationChannel = new NotificationChannel("","",NotificationImportance.High);

它还允许用户直接在手机上设置通知的重要性级别。可以实现紧急暂停。

对于8.0之前的Android版本,将SetFullScreenIntent添加到notificationBuilder

notificationBuilder.SetFullScreenIntent(pendingIntent,true); 

或激活声音和振动提示。

答案 1 :(得分:0)

这可能是一个远景,但经过大量挖掘,我发现在后台,尤其是在被刷掉时,您无权访问XamarinForms对象,因为只有在您实际执行start活动后,该对象才会被实例化。 / p>

例如如果这实际上是在使用DependencyService或其他Forms库,则会收到错误消息。

CrossBadge.Current.SetBadge(_notificationBadgeCount)

还请确保从通知服务器将默认优先级设置为high,默认情况下这是正常的,如果将其清除,不会导致应用程序唤醒。

priority: "high",
android: {
    priority: "high",
    notification: {
        title: title,
        body: body,
        sound: "default",
        priority: "high",
        sticky: false,
        defaultSound: true,
        defaultVibrateTimings: true,
    },
    data: {        
       title: title,
       body: body
   }
}