FCM - 标头中未显示消息

时间:2016-09-02 09:17:52

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

我正在尝试在我的应用上实施FCM。教程来源来自here

推送通知已成功发送,但为什么标题中不显示消息? (就像收到WhatsApp消息时一样)。它只显示后面列表中的静默向下滚动标题后)。此外,推送通知不会产生任何声音。

这是我的代码:

AndroidManifest.xml

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

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

FCMMessageReceiverService.java

public class FCMMessageReceiverService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d("Notification Received",remoteMessage.getNotification().getBody());

    sendNotification(remoteMessage.getNotification().getBody());
    sendSnackbar(remoteMessage.getNotification().getBody());
}

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MyActivity.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);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(messageBody)
            .setAutoCancel(false)
            .setSound(defaultSoundUri);

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

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

 private void sendSnackbar(String messagebody)
 {


 }
}

TokenService.java

public class TokenService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        //super.onTokenRefresh();
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.w("notification", refreshedToken);
        sendRegistrationToServer(refreshedToken);
    }
    private void sendRegistrationToServer(String token) {
    }
}

1 个答案:

答案 0 :(得分:0)

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

只需添加两个属性android:enabledandroid:exported,如上所示AndroidManifest.xml