当前位置: 首页 > 知识库问答 >
问题:

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

丁曦
2023-03-14

我通过FCM实现了推送通知。当应用程序从我的服务器获得一个新的通知时,通知面板会被我在notificationcompat.builder中设置的图标所注意,但消息不会作为弹出预览。我尝试设置优先级,样式,类别,但通知仍然没有显示。当我滚动时,我可以看到通知。

我在两种不同的设备操作系统(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>

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());

   }
}

后端(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
                }
            };

共有1个答案

宓跃
2023-03-14

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

 类似资料:
  • 我想为一个聊天应用程序实现FCM推送通知服务,我遵循Firebase文档中的步骤,当通知从Firebase控制台发送到我的设备时,我会得到通知。当我尝试使用http post to https://FCM.googleapis.com/FCM/send通过FCM将通知通过服务器端发送到设备时: 当应用程序处于活动状态并且我正在将此通知发送到我的设备时,Im在我的控制台日志中收到以下消息,所以我认为

  • 以下是我的通知案例:当通知到达时,应用程序可能是前台,后台,或关闭 2.后台:当用户点击通知时,应用程序应显示在前面,并显示主要活动 3.App关闭时:当用户点击通知时,App应打开,并显示主要活动。 当我用设置intent和用设置PendingIntent时。它按预期工作,但当用户点击notification时,它不会调用Main Activity的onCreate方法。 如果您考虑了上面的代码

  • 我正在rails 5中编写一个Api Rest,使用gem'fcm'发送通知。我已经在android应用程序中配置了firebase,我可以从firebase控制台成功发送通知,但从rails api我无法在设备中接收通知,这是我的代码: 这是我的rails控制器: 我从邮递员执行这是执行控制器的路线: http://localhost:3000/acciones/enviar?here 将设备令

  • 几个小时前,我通过Github网站创建了两个存储库。然后,我访问了Github桌面应用程序,并对这两个存储库中的每一个都做了许多promise。我所遵循的程序是: 在Github网站上创建存储库 转到Github桌面应用程序,点击“克隆”以获得所选存储库 存储库已克隆到“Documents”中的“GitHub”文件夹 然后将代码文件复制到每个存储库文件夹(通过Windows资源管理器) 在Gith

  • 当我从Firebase发送推送时,如果应用程序在后台或关闭,我会收到通知,但当应用程序打开时,不是... 有什么建议,如何生成通知时,应用程序是前景?

  • 我是新来的< code>Android Studio,所以我不明白。我是使用< code>FCM的< code >推送通知的新手,但我没有得到解决方案。我在运行时遇到了这种错误。 我的应用程序/Gradle 这是我的项目等级 我已经更新了android studio<code>SDK</code>我这边什么都没有了,但我不知道我哪里出错了。所以请在这方面帮助我。