我的通知数据消息如下:
{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)
中收到。我也知道我的这条消息只有在应用程序处于前台时才会收到。
问题
但是当应用程序在后台或关闭时,通知会出现在通知托盘中。问题是当我点击通知时,它没有启动我的应用程序,完全没有响应。
这是我的代码:
安卓清单.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天里,我一遍又一遍地查看相同的代码后,我仍然无法弄清楚问题来自哪里。有人请帮忙..
如果可能,请给出一些提示,说明可能导致此问题的可能问题是什么。
谢谢。
如果你能告诉我你从哪里调用这个 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((类的类,我将获得清晰的视图。
希望对您有所帮助。