Firebase通知Android:onMessageReceived无法在真实设备上运行

时间:2017-03-24 17:04:54

标签: android android-studio firebase notifications

最近我正在使用一个带有来自firebase的消息服务的android项目,我检查了所有教程但是我没有成功...我不知道会发生什么......这就是我得到的现在......

清单文件

    <application>
         ...
        <service
        android:name=".MFireBaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>you
    </service>

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

</application>

应用程序/的build.gradle

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'

compile files('libs/ksoap2-android-assembly-3.0.0-RC.4-jar-with-dependencies.jar')
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'com.android.support:support-v4:25.0.1'
compile 'com.google.android.gms:play-services:10.0.1'
compile 'com.google.firebase:firebase-messaging:9.6.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-auth:9.6.1'
}
apply plugin: 'com.google.gms.google-services'

项目/的build.gradle

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

MFireBaseInstanceIDService类

import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class MFireBaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG= "FirebaseInsIDService";

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

}

}

MFirebaseMessagingService类

    import android.app.NotificationManager;
import android.app.PendingIntent;
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 MFireBaseMessagingService extends FirebaseMessagingService 

private static final String TAG= "FireBaseMsgService";


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    if(remoteMessage.getData().size()>0)
    {
        Log.d(TAG, "Message data: " + remoteMessage.getData());
        sendNotification(remoteMessage.getData().toString());
    }


if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Body: " +                         remoteMessage.getNotification().getBody());
    }

}

我已将json文件导入app目录..我将感激各种帮助!谢谢!

0 个答案:

没有答案