Android应用程序未收到Firebase通知。

3
我的安卓应用没有收到任何Firebase通知。当我从Firebase控制台发送消息时,它显示进程已完成,但在展开行时却显示0条消息发送,如下截图所示: Screenshot 消息的过期时间设置为1分钟。
我已添加了FirebaseMessagingService、FirebaseInstanceIdService,并像这样在清单中添加了服务:
    <service android:name=".service.TjssFireBaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service
        android:name=".service.TjssFireBaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

我尝试添加了 < /p>
android:enabled="true"
android:exported="true"

这是应用程序类的onCreate方法。
@Override
public void onCreate() {
    super.onCreate();
    mGlide = Glide.with(this);

    String token = FirebaseInstanceId.getInstance().getToken();
    if (token != null)
        M.log("Firebase Token", token);
}

在logcat输出中,我有一个有效的令牌。
我按照文档中所说的一切去做了,但它不起作用。我错过了什么重要的东西吗?

请分享您完整的清单和应用文件,您是否在应用文件中初始化了FirebaseInstance? - Rishabh Dugar
我已经编辑了我的问题并添加了应用程序类的onCreate方法。如何初始化Firebase实例? - Sony
3个回答

1
在你的Application的onCreate方法中添加以下代码。
FirebaseApp.initializeApp(this);

这里是意义的上下文


0

确保在发送通知时您的应用程序未在运行。如果您的应用程序在前台,则无法接收通知。

如果您希望在应用程序在前台时接收通知,请按以下方式编辑您的服务:

public class TjssFireBaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if (remoteMessage.getNotification() != null) {
        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        i.putExtra("Val", remoteMessage.getNotification().getTitle());
        startActivity(i);
    }
}

@Override
public void onDeletedMessages() {
    super.onDeletedMessages();
}}

当收到通知并且您的应用程序在前台时,上述代码将启动您的主要活动。


0

你是否将google-services.json放在你的应用文件夹中?如果没有,请将它放在app文件夹中,并查看此文件中的信息是否与你的项目相关。尝试通过选择你的应用程序包名称来发送通知。我希望这会起作用。谢谢。


请检查您的包名。 - chandrakant sharma
将包从清单复制到 Firebase 控制台。 - Sony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接