没有收到来自firebase的通知

时间:2017-09-22 07:18:43

标签: android firebase firebase-cloud-messaging firebase-notifications

我有一个安装了firebase的Android应用程序。

  • 我在开始时已成功收到firebase注册ID。

但是当我将消息从firebase控制台发送到我的应用程序时,我没有在手机中收到它

这是我的app gradle

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.google.firebase:firebase-auth:11.0.4'
compile 'com.android.support:design:25.3.1'
compile 'com.google.firebase:firebase-core:10.2.1'
compile 'com.google.firebase:firebase-messaging:10.2.1'

构建gradle

classpath 'com.google.gms:google-services:3.0.0'

我有一个单独的类MyFirebaseMessagingService,它扩展了FirebaseMessagingService

 public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "From: " + remoteMessage.getFrom());
    Log.e("tagg", "From: " + remoteMessage.getFrom());

    if (remoteMessage == null)
        return;

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
        handleNotification(remoteMessage.getNotification().getBody());
    }

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }

我没有在这堂课上登录。

如何在错误发生的部分发生,我已经成功收到了reg id,那么错误是什么

3 个答案:

答案 0 :(得分:0)

您是否已将该服务添加到AndroidManifest?

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

答案 1 :(得分:0)

将服务添加到AndroidManifest

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

答案 2 :(得分:0)

private void HandleNotification(String messagebody) {
    Intent intent = new Intent(this, your.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);
    android.support.v4.app.NotificationCompat.Builder notificationBuilder = new android.support.v4.app.NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.logo)
            .setContentTitle("")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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