FCM消息未显示在状态栏

时间:2017-12-14 07:10:29

标签: android firebase firebase-cloud-messaging

我已按照FCM文档设置Android应用以接收FCM通知。 我的app build.gradle如下:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "27.0.1"
defaultConfig {
    applicationId "np.com.ravi.fcmtest"
    minSdkVersion 15
    targetSdkVersion 26
    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:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'

testCompile 'junit:junit:4.12'
//for firebase notifications
compile 'com.google.firebase:firebase-messaging:10.0.1'
}

//for firebase notifications
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.3.3'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    //for firebase notifications
    classpath 'com.google.gms:google-services:3.0.0'
}
}

allprojects {
repositories {
    jcenter()
    maven {
        url "https://maven.google.com" // Google's Maven repository
    }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

我有一个基本没有代码的MainActivity

我有两项服务:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";

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

    // TODO: Implement this method to send any registration to your app's servers.
    sendRegistrationToServer(refreshedToken);
}

/**
 * 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) {
    // Add custom implementation, as needed.
}
}

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";

@Override
public void onDeletedMessages() {
    Log.d(TAG, "Notification message deleted");
}

@Override
public void onSendError(String s, Exception e) {
    Log.d(TAG, s);
}

@Override
public void onMessageSent(String s) {
    super.onMessageSent(s);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO: Handle FCM messages here.
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

    //createNotification(remoteMessage.getNotification().getBody());
    // Create Notification
    Log.d("On", "createNotification");

    //for when the notification is tapped we are directed to the MainActivity
    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);

    //setting notification attributes
    Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    String CHANNEL_ID = "my_channel_01";
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle("FCM Test")
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);

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

    //post notification to notification bar
    notificationManager.notify(0, notificationBuilder.build());

}
}

我还在AndroidManifest.xml

中注册了这两项服务
<service android:name=".MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

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

我应该收到firebase通知。但我在LogCat中没有收到任何消息。无论我的应用程序是在前台还是在后台,我也没有将它们作为通知提供给我的状态栏。

我在Logcat中收到的消息是:

12-14 12:40:46.140 32287-32287/np.com.ravi.fcmtest D/AppTracker: App Event: stop
12-14 12:40:46.218 32287-2094/np.com.ravi.fcmtest V/FA: Recording user engagement, ms: 78587
12-14 12:40:46.220 32287-2094/np.com.ravi.fcmtest V/FA: Using measurement service
12-14 12:40:46.220 32287-2094/np.com.ravi.fcmtest V/FA: Connecting to remote service
12-14 12:40:46.224 32287-2094/np.com.ravi.fcmtest V/FA: Activity paused, time: 949735552
12-14 12:40:46.234 32287-2094/np.com.ravi.fcmtest I/FA: Tag Manager is not found and thus will not be used
12-14 12:40:46.240 32287-2094/np.com.ravi.fcmtest D/FA: Logging event (FE): _e, Bundle[{_o=auto, _et=78587, _sc=MainActivity, _si=-8670171141235923720}]
12-14 12:40:46.269 32287-2094/np.com.ravi.fcmtest V/FA: Using measurement service
12-14 12:40:46.269 32287-2094/np.com.ravi.fcmtest V/FA: Connection attempt already in progress
12-14 12:40:46.270 32287-2094/np.com.ravi.fcmtest D/FA: Connected to remote service
12-14 12:40:46.270 32287-2094/np.com.ravi.fcmtest V/FA: Processing queued up service tasks: 2
12-14 12:40:51.313 32287-2094/np.com.ravi.fcmtest V/FA: Inactivity, disconnecting from the service

请帮我解决这个问题。感谢。

2 个答案:

答案 0 :(得分:0)

你可以添加 firebase-core 必须获取firebase通知和消息。在你的gradle中

compile 'com.google.firebase:firebase-core:10.0.1'    

答案 1 :(得分:-1)

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.e(TAG, "From: " + remoteMessage.getData().toString());
}

写下此行并检查通知数据是否正在