我已经在android清单中实现了该服务,但 FirebaseMessagingService 服务仍然没有响应。奇怪的是,通知已成功发送。当我尝试调试并将断点添加到 onMessageReceived 方法时,我可以收到通知,但是当我在不附加调试的情况下首先安装该通知时,该服务不会被调用,因此 FirebaseMessagingService没有回调响应。因此,如果我调试应用程序,基本上 FirebaseMessagingService 可以正常工作,但是当它不在调试状态时,不会从 FirebaseMessagingService 调用任何回调。
我使用的手机是Samsung galaxy J7pro android版本OREO(API 27)。
AndroidManifest.xml 在清单文件中添加了以下内容。
<service android:name=".services.InstanceIDListenerService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".services.NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
build.gradle (应用程序) 在build.gradle文件中添加了以下内容。
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.firebase:firebase-core:16.0.5'
}
apply plugin: 'com.google.gms.google-services'
NotificationService.java
public class NotificationService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// dbManagerPushNotification.DBManagerPushNotification(this);
if (remoteMessage.getData().size() > 0) {
Map<String, String> remoteMessage_getData = remoteMessage.getData();
String community_slug = remoteMessage_getData.get("community_slug");
String conversation_id = remoteMessage_getData.get("conversation_id");
String sender_id = remoteMessage_getData.get("sender_id");
final String message_id = remoteMessage_getData.get("message_id");
String message = remoteMessage_getData.get("message");
String convo_type = remoteMessage_getData.get("convo_type");
String created_at = remoteMessage_getData.get("created_at");
String title = remoteMessage_getData.get("title");
Log.i("push_notification", "onMessageReceived: " + message);
final String channel_id = title + community_slug;
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("message", message);
intent.putExtra("message_id", message_id);
intent.putExtra("conversation_id", conversation_id);
intent.putExtra("sender_id", sender_id);
intent.putExtra("community_slug", community_slug);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), message_id.hashCode(), intent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
SpannableStringBuilder spannableStringBuilder_title = new SpannableStringBuilder(title);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
spannableStringBuilder_title.setSpan(boldSpan, 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channel_id, title, NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
mChannel.enableLights(true);
mChannel.setLightColor(Color.WHITE);
assert notificationManager != null;
notificationManager.createNotificationChannel(mChannel);
}
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), channel_id)
.setSmallIcon(R.drawable.ic_notif_logo)
.setColor(0xECEFF1)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setGroup(convo_type)
.setGroupAlertBehavior(GROUP_ALERT_CHILDREN)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.selection_orange))
.setPriority(NotificationCompat.PRIORITY_MAX);
String community_notif = " " + community_slug + ": ";
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(community_notif);
spannableStringBuilder.setSpan(new ForegroundColorSpan(getApplicationContext().getResources().getColor(R.color.selection_orange)), 0, community_notif.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
NotificationCompat.Builder summaryNotification = new NotificationCompat.Builder(getApplicationContext(), channel_id)
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_notif_logo)
.setStyle(new NotificationCompat.InboxStyle().setSummaryText(TextUtils.concat(spannableStringBuilder, title)).setBigContentTitle(TextUtils.concat(spannableStringBuilder, title)))
.setGroup(convo_type)
.setAutoCancel(true)
.setGroupAlertBehavior(GROUP_ALERT_CHILDREN)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.selection_orange))
.setGroupSummary(true)
.setPriority(NotificationCompat.PRIORITY_MAX);
assert notificationManager != null;
notificationManager.notify(message_id.hashCode(), mBuilder.build());
notificationManager.notify(channel_id.hashCode(), summaryNotification.build());
}
}
}