2017-01-24 4 views
2

Ich habe ein Problem beim Senden von Push-Benachrichtigungen an ein Android-Gerät über die Firebase-Konsole. Specifically, Firebase is not attempting to send the notification. Das verknüpfte Bild zeigt, was ich von der Konsole aus sehe, wenn ich die Benachrichtigung sende (vollständiger Status, Lieferdatum, das der Sendezeit entspricht, aber nichts gesendet wurde).Firebase sendet keine Benachrichtigungen von der Konsole

Ich entdeckte dieses Problem zunächst in meiner eigenen App, aber ging weiter zum Testen des Problems mit Firebase's demonstration app, aber ohne Erfolg. Ich kann FCM-Registrierungstoken ohne Probleme abrufen, daher besteht eine Verbindung zu Firebase. Ich teste hauptsächlich auf einem Emulator in Android Studio mit API 25, aber ich habe auch auf Emulatoren mit APIs 17 und 23 sowie persönliche Android-Geräte mit APIs 17 und 23 getestet. Ich habe eine Vielzahl von Firebase/Google ausprobiert Spielen Sie Versionen von 9.0.2 bis zur aktuellen Version 10.0.1. Ich habe auch keinen Code in der Firebase Demo-App geändert (außer Abhängigkeiten in build.gradle und dem Manifest in Versuchen, mein Problem zu lösen).

Was dieses Problem noch seltsamer macht ist, dass diese Benachrichtigungen schließlich senden, aber es dauert ungewöhnlich lange und es ist immer, nachdem die App/Emulator geschlossen wurde, so dass ich sie nie wirklich gesehen habe. Manchmal dauert es ein paar Stunden, aber oft ist es erst am nächsten Tag, wenn ich auf die Konsole schaue und sehe, dass sie gesendet wird. Anfangs habe ich dies mit einer einfachen Verzögerung angekündigt, aber ich habe in den letzten anderthalb Wochen mehr als 50 Testbenachrichtigungen gesendet und alle von ihnen hatten dieses Problem.

Dies ist, was die einzigen Dateien, die ich alle Änderungen auf aktuell aussehen (der jüngste Test Benachrichtigung an einen Emulator mit API 25 gesendet wurde):

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.google.firebase.quickstart.fcm"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme"> 
    <!-- [START fcm_default_icon] --> 
    <!-- Set custom default icon. This is used when no icon is set for incoming notification messages.--> 
    <meta-data 
     android:name="com.google.firebase.messaging.default_notification_icon" 
     android:resource="@drawable/ic_stat_ic_notification" /> 
    <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming 
     notification message.--> 
    <meta-data 
     android:name="com.google.firebase.messaging.default_notification_color" 
     android:resource="@color/colorAccent" /> 
    <!-- [END fcm_default_icon] --> 
    <activity 
     android:name="com.google.firebase.quickstart.fcm.MainActivity" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"/> 
      <category android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 
    </activity> 

    <!-- [START firebase_service] --> 
    <service 
     android:name=".MyFirebaseMessagingService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
    </service> 
    <!-- [END firebase_service] --> 
    <!-- [START firebase_iid_service] --> 
    <service 
     android:name=".MyFirebaseInstanceIDService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
     </intent-filter> 
    </service> 
    <!-- [END firebase_iid_service] --> 
</application> 

Projekt build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules. 

buildscript { 
    repositories { 
     jcenter() 
     mavenLocal() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.2.3' 
     classpath 'com.google.gms:google-services:3.0.0' 

     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
} 

allprojects { 
    repositories { 
     jcenter() 
     mavenLocal() 
    } 
} 

App build.gradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.0" 

    defaultConfig { 
     applicationId "com.google.firebase.quickstart.fcm" 
     minSdkVersion 14 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 

     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 

buildTypes { 
    release { 
     minifyEnabled true 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 

    packagingOptions { 
     exclude 'LICENSE.txt' 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:25.0.1' 

    compile 'com.google.firebase:firebase-messaging:9.6.0' 

    // Testing dependencies 
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' 
    androidTestCompile 'com.android.support.test:runner:0.5' 
    androidTestCompile 'com.android.support:support-annotations:25.0.1' 
} 

apply plugin: 'com.google.gms.google-services' 

Jede Hilfe oder Einsicht jemand würde könnte liefern werden viel appreicated. Vielen Dank!

EDIT 1:

MyFirebaseInstanceIDService.java

Auf Wunsch sind hier die Service-Implementierungen. Es sollte beachtet werden, dass sie von der Lager-Version in Absatz 2.

/** 
* Copyright 2016 Google Inc. All Rights Reserved. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

package com.google.firebase.quickstart.fcm; 

import android.util.Log; 

import com.google.firebase.iid.FirebaseInstanceId; 
import com.google.firebase.iid.FirebaseInstanceIdService; 


public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 

private static final String TAG = "MyFirebaseIIDService"; 

/** 
* Called if InstanceID token is updated. This may occur if the security of 
* the previous token had been compromised. Note that this is called when the InstanceID token 
* is initially generated so this is where you would retrieve the token. 
*/ 
// [START refresh_token] 
    @Override 
    public void onTokenRefresh() { 
     // Get updated InstanceID token. 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
     Log.d(TAG, "Refreshed token: " + refreshedToken); 

     // If you want to send messages to this application instance or 
     // manage this apps subscriptions on the server side, send the 
     // Instance ID token to your app server. 
     sendRegistrationToServer(refreshedToken); 
    } 
    // [END refresh_token] 

    /** 
    * Persist token to third-party servers. 
    * 
    * Modify this method to associate the user's FCM InstanceID token with any server-side account 
    * maintained by your application. 
    * 
    * @param token The new token. 
    */ 
    private void sendRegistrationToServer(String token) { 
     // TODO: Implement this method to send token to your app server. 
    } 
} 

MyFirebaseMessagingService.java verknüpft sind unverändert

/** 
* Copyright 2016 Google Inc. All Rights Reserved. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

package com.google.firebase.quickstart.fcm; 

import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

import com.google.firebase.messaging.FirebaseMessagingService; 
import com.google.firebase.messaging.RemoteMessage; 

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    /** 
    * Called when message is received. 
    * 
    * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
    */ 
    // [START receive_message] 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // [START_EXCLUDE] 
     // There are two types of messages data messages and notification messages. Data messages are handled 
     // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type 
     // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app 
     // is in the foreground. When the app is in the background an automatically generated notification is displayed. 
     // When the user taps on the notification they are returned to the app. Messages containing both notification 
     // and data payloads are treated as notification messages. The Firebase console always sends notification 
     // messages. 
     // [END_EXCLUDE] 

     // TODO(developer): Handle FCM messages here. 
     // Not getting messages here? See why this may be: 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 

     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
     } 

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

     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. See sendNotification method below. 

     sendNotification(remoteMessage.getNotification().getBody()); 
    } 
    // [END receive_message] 

    /** 
    * Create and show a simple notification containing the received FCM message. 
    * 
    * @param messageBody FCM message body received. 
    */ 
    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 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_stat_ic_notification) 
      .setContentTitle("FCM Message") 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
} 

EDIT 2:

Ich sollte hinzufügen, dass onMessageReceived ist nie Ausgelöst.

+0

Veröffentlichen Sie Ihre Service-Implementierungen, so können wir sehen, was los ist. –

+0

@HristoStoyanov Ich habe es hinzugefügt, obwohl ich beachten sollte, dass es nicht anders ist als die Aktienversion, die ich verknüpfte. – David

+0

Warum verwenden Sie ältere Version der Bibliothek, versuchen Sie mit 10.0.1 –

Antwort

0

In Ihren Abhängigkeiten in Build.gradle fügen Sie folgendes:

//Used for firebase services 
compile 'com.google.firebase:firebase-core:10.0.1' 

EDIT:

Normalerweise mit der folgenden in Ihrer App build.gradle

//Used for firebase services 
compile 'com.google.firebase:firebase-core:10.0.1' 
//Used for push notification services 
compile 'com.google.firebase:firebase-messaging:10.0.1' 

und die folgenden in Ihrem root build.gradle

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

Und die Google-Services.json, die Sie generiert haben, geben Sie Ihren Paketnamen und die Sha1 Ke y Sie sollten in der Lage sein, Push-Benachrichtigungen von Firebase zu erhalten. Überprüfen Sie noch einmal, ob Sie die richtige SHA1 Schlüssel kopiert haben und nicht die SHA256 noch die MDA5 Schlüssel

+0

Leider habe ich das schon ohne Glück versucht. – David

+0

Haben Sie die generierte Datei google-services.json in Ihren App-Ordner kopiert? – thushcapone

+0

Ich tat, das war eines der ersten Dinge, die ich suchte – David

Verwandte Themen