Android应用程序通过FCM获取推送通知但不显示消息

kok*_*oko 5 android push-notification firebase firebase-cloud-messaging

我通过FCM实现了推送通知。当应用程序从服务器收到新通知时,“通知面板”条会被我在NotificationCompat.Builder中设置的图标所注意到,但是该消息不会作为弹出窗口预览。我尝试设置优先级,样式,类别,但仍未显示通知。滚动时可以看到通知。

我在2种不同的设备操作系统(6.0.1&5.0.1)上尝试了该应用程序,也是我的后端C#解决方案-两种方法都不会弹出通知-消息和通知

表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theApp"
android:versionCode="31"
android:versionName="0.3.4"  >
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="23" />
   <uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission    android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity
        android:name="com.theApp.SplashScreen"
        android:screenOrientation="portrait"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>



    <activity
        android:name="com.theApp.MainView"
        android:windowSoftInputMode="adjustResize"
     android:screenOrientation="portrait"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.NoActionBar" >
    </activity>


    <service android:name=".FirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service android:name=".FirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>


</application>




</manifest>
Run Code Online (Sandbox Code Playgroud)

FirebaseMessagingService

package com.theApp;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessagingService extends    com.google.firebase.messaging.FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    showNotification(remoteMessage.getData().get("message"));
}
public void showNotification(String message)
{
    Intent i =new Intent(this,MainView.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ); //FLAG_ACTIVITY_CLEAR_TOP

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_launcher)
            .setAutoCancel(true)
            .setContentTitle("Title")
            .setStyle(new NotificationCompat.BigTextStyle()
            .bigText(message))
            .setContentText(message)
            .setTicker(message)
            .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentIntent(pendingIntent)
            .setCategory(NotificationCompat.CATEGORY_PROMO);

        NotificationManager manager =   (NotificationManager)getApplicationContext().getSystemService(Context.NOTI      FICATION_SERVICE);
        manager.notify(0,builder.build());

   }
}
Run Code Online (Sandbox Code Playgroud)

后端(C#):

// Data message
  var contentData = new
            {
                registration_ids = tokens,
                priority = "high",
                data = new
                {                       
                    title = "this is title",
                    body = "this is body"
                }
            };

            // Notification message
            var contentData = new
            {
                registration_ids = tokens,
                priority = "high",
                notification = new
                {
                    title = title,
                    body = message
                }
            };
Run Code Online (Sandbox Code Playgroud)

TMt*_*ech 1

如果您想读取通知,则必须调用remoteMessage.getNotification()而不是调用remoteMessage.getData()。请注意,'notification'密钥应该在服务器发送的有效负载中可用,以便将其解析为通知。getData()仅当您的服务器发送数据内容时才会返回 Map。有关更多信息,请参阅FCM 指南