我在Android应用上设置了FCM客户端。当我尝试从Firebase控制台推送通知时,控制台会显示该消息已发送。
但我无法在设备上看到任何通知。 我正在关注Android Logcat上的日志:
03-26 14:22:26.607 20227-31218/co.pushmessages V/FA: Connecting to remote service
03-26 14:22:26.617 20227-31218/co.pushmessages D/FA: Logging event (FE): _nf, Bundle[{_o=fcm, _sc=MainActivity, _si=3365666155092549436, _ndt=0, _nmn=msg, _nmt=1490518346, _nmid=8754479537222765642}]
03-26 14:22:26.637 20227-31218/co.pushmessages V/FA: Using measurement service
03-26 14:22:26.637 20227-31218/co.pushmessages V/FA: Connection attempt already in progress
03-26 14:22:26.637 20227-31218/co.pushmessages D/FA: Connected to remote service
03-26 14:22:26.637 20227-31218/co.pushmessages V/FA: Processing queued up service tasks: 2
03-26 14:22:29.277 20227-20233/co.pushmessages D/dalvikvm: Debugger has detached; object registry had 1 entries
03-26 14:22:31.667 20227-31218/co.pushmessages V/FA: Inactivity, disconnecting from the service
有没有办法让这些消息在deice上传递?
我在app文件夹中放置了正确的google-services.json,并且所有签名都是正确的。
我的 app level build.gradle 是
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "co.pushmessages"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.google.firebase:firebase-messaging:10.2.1'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
我的项目级别build.gradle 是:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:3.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我的清单文件是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.pushmessages">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
版本:
Android Studio Ver 2.2.3
Android OS(设备)Ver 4.4.4
修改
MyFirebaseInstanceIdService.java
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public MyFirebaseMessagingService() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// 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.
Log.e(TAG, "From: " + remoteMessage.getFrom());
Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}
修改
按照水银的建议对Manifest文件进行了一些更改,现在它运行顺畅。以下是使其工作的最终清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.pushmessages">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TargetActivity"></activity>
<service android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
答案 0 :(得分:0)
根据FireBase(android)示例, 首先,我在你的Manifest中看不到以下行为:
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
其次,我不知道/你没有发布你的onMessageReceived(RemoteMessage remoteMessage)
- 是否被称为?
请浏览FCM指南: