【问题标题】:FireBase Cloud Messaging Not WorkingFireBase 云消息传递不起作用
【发布时间】:2016-05-27 07:43:55
【问题描述】:

我正在尝试使用 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 cloud messaging notification not received by device 但它仍然无法正常工作。

【问题讨论】:

  • 您是通过控制台发送有效载荷吗?
  • @McAwesomville 是的,它与他们的样本一起工作
  • 对不起。我之前没看到,你已经提到了。无论如何,所以您的onMessageReceived() 中的日志在您发送通知后没有显示?应用程序是在前台还是后台?
  • 日志未显示,应用程序正在前台运行
  • 这很奇怪。你确定你指定了正确的registration token吗?

标签: android firebase firebase-cloud-messaging


【解决方案1】:

尝试使用服务 api 调用发送消息。它将为您提供响应代码和响应 json,这可能会帮助您了解 FCM 不工作的原因。

这里回答了服务调用使用情况-Firebase onMessageReceived not called when app in background

【讨论】:

    【解决方案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>
    

    如果它不起作用,请告诉我。

    【讨论】:

      猜你喜欢
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2018-01-09
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多