为什么单击托盘中的FCM通知未启动我的应用程序?

时间:2018-06-02 03:12:12

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

我的通知数据信息如下:

{data=
    {"sender_username":null,
        "image":null,
        "is_background":false,
        "payload":{"post_id":"1"},
        "userId":2,
        "title":"ABC",
        "message":"Someone liked your post!",
        "timestamp":"2018-06-02 10:46:50"
    }
}

当应用程序在前台时收到此消息,它在onMessageReceived(RemoteMessage message)中收到。我也知道我的此消息仅在应用程序处于前台时收到。

问题

但是当应用程序处于后台或关闭时,通知会显示在通知托盘中。问题是当我点击通知时,它不会启动我的应用程序,完全没有响应。

这是我的代码:

AndroidManifest.xml

<!-- Firebase service -->
     <service
        android:name=".services.FirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
    </service>
    <service
        android:name=".services.FirebaseInstanceIDService"
        android:enabled="true"
        android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
    </service>

以下是我处理通知的方式:

public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {
    // Check for empty push message
    if (TextUtils.isEmpty(message))
        return;


    // notification icon
    final int icon = R.drawable.logo;

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    mContext,
                    0,
                    intent,
                    PendingIntent.FLAG_CANCEL_CURRENT
            );

    final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            mContext);

    final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
            + "://" + mContext.getPackageName() + "/raw/notification");

    if (!TextUtils.isEmpty(imageUrl)) {

        if (!imageUrl.equalsIgnoreCase(null) && imageUrl.length() > 4 && Patterns.WEB_URL.matcher(imageUrl).matches()) {

            Bitmap bitmap = getBitmapFromURL(imageUrl);

            if (bitmap != null) {
                showBigNotification(bitmap, mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
            } else {
                showSmallNotification(mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
            }
        }
    } else {
        showSmallNotification(mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
        playNotificationSound();
    }
}

private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

    inboxStyle.addLine(message);

    Notification notification;
    notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            .setSound(alarmSound)
            .setStyle(inboxStyle)
            .setWhen(getTimeMilliSec(timeStamp))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
            .setContentText(message)
            .build();

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(AppConfig.NOTIFICATION_ID, notification);
}

我的代码在'com.google.firebase:firebase-messaging:10.2.4'之前运行良好,当我升级到'com.google.firebase:firebase-messaging:17.0.0'时会出现问题。

在我过去2天一次又一次地查看相同的代码之后,我仍然无法弄清楚问题来自哪里。有人请帮助..

如果可能,请提供一些提示,说明导致此问题的可能问题是什么。

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您可以告诉我您在哪里调用此showNotification消息,以及您的FirebaseMessagingService()类在哪里,我可以提供更好的建议?

但据我所知,您作为参数传递的意图应指定您要打开的活动,如下所示:

    val intent = Intent(this@MyFirebaseMessagingService, MainActivity::class.java)// here i want to open the MainActivity on notification click
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val pendingIntent = PendingIntent.getActivity(this@MyFirebaseMessagingService, 0, intent, PendingIntent.FLAG_ONE_SHOT)

如果您可以向我展示您扩展FirebaseMessagingService()类的课程,我会得到一个清晰的视图。

希望它有所帮助。