FireBase云消息推送无法工作

8
我正在尝试使用 FireBase 云消息推送。我已经收到了一个令牌,但是没有从控制台接收到任何通知。以下是我的清单文件:
  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="careerage.jobseeker.app"
android:hardwareAccelerated="true"
android:versionCode="1"
android:versionName="0.0.1">

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="23" />

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:resizeable="true"
    android:smallScreens="true"
    android:xlargeScreens="true" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:hardwareAccelerated="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:supportsRtl="true">
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
        android:label="@string/activity_name"
        android:launchMode="singleTop"
        android:theme="@android:style/Theme.DeviceDefault.NoActionBar"
        android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/launcher_name">
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".TokenService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <service
        android:name=".NotificationService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
</application>

我的代码几乎和样例中的一模一样:

public class NotificationService extends FirebaseMessagingService {

private void sendNotification(String messagebody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("Notified")
            .setContentText(messagebody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 , notificationBuilder.build());
}

private void sendSnackbar(String messagebody)
{
    Toast.makeText(this,"Notified",Toast.LENGTH_LONG).show();
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Toast.makeText(this,"Message Received",Toast.LENGTH_LONG).show();

        Log.d("Notification Received",remoteMessage.getNotification().getBody());

        sendNotification(remoteMessage.getNotification().getBody());
    sendSnackbar(remoteMessage.getNotification().getBody());
}

}

我查看了这里的问题Firebase云消息通知未被设备接收,但仍然无法正常工作。


你是通过控制台发送有效载荷吗? - AL.
@McAwesomville 是的,它正在使用他们的示例工作。 - Yash Jain
抱歉,我之前没有看到你已经提到了。无论如何,在你发送通知后,onMessageReceived() 中的日志没有显示出来吗?应用程序是在前台还是后台运行? - AL.
日志中是否有任何错误? - AL.
如果消息发送到特定令牌时失败,这很可能意味着该令牌无效/未正确与您的应用程序关联。您能否再次确认一下您获取该令牌的方式?同时,请检查是否已将正确的软件包名称添加到 Firebase 控制台,并下载了 google-services.json 文件。最后,请尝试卸载并重新安装应用程序,并获取新的令牌。 - Diego Giorgini
显示剩余5条评论
2个回答

1

-2

您需要将服务更改为完整的包名称,如下所示:

<service
    android:name="your.package.name.TokenService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>
<service
    android:name="your.package.name.NotificationService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

如果它不能正常工作,请告诉我。


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