Firebase onMessageReceived在后台应用时没有调用

时间:2016-06-14 08:15:23

标签: android android-studio push-notification firebase

我的问题是,onMessageReceived事件仅在app处于前台时被调用,在这种情况下,我能够以所需的样式触发我想要的通知。有没有办法在应用程序处于后台时检查哪个事件监听器,我可以触发相同的样式通知而不是默认通知。谢谢。

以下是我的服务类

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) {
    // 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.d(TAG, "From: " + remoteMessage.getFrom());
    //Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}
// [END receive_message]

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */
private void sendNotification(String title,String messageBody) {
//                     Toast.makeText(this.getApplicationContext(),messageBody,Toast.LENGTH_SHORT).show();
    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);
    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(getNotificationIcon())
            .setContentTitle(getResources().getString(R.string.app_name))
            .setContentText(messageBody)
            .setLargeIcon(largeIcon)
            .setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(false)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(messageBody))
            .setSound(defaultSoundUri)
            .setLights(Color.RED, 300, 300)
            .setVibrate(new long[] { 100, 250 })
            .setContentIntent(pendingIntent);

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

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

}

这是两种服务的清单代码

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

0 个答案:

没有答案