gpt4 book ai didi

android - Firebase 云消息在后台崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:20:26 27 4
gpt4 key购买 nike

最近遇到一个奇怪的crash,是在收到firebase cloud消息时发生的,不确定是不是app在后台时从firebase控制台发送通知导致的,这里是日志

Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'int com.android.okhttp.okio.Segment.limit' on a null object reference
at com.android.okhttp.okio.Buffer.write + 1182(Buffer.java:1182)
at com.android.okhttp.okio.Buffer.read + 1221(Buffer.java:1221)
at com.android.okhttp.okio.RealBufferedSource.read + 55(RealBufferedSource.java:55)
at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read + 418(HttpConnection.java:418)
at com.android.okhttp.internal.Util.skipAll + 176(Util.java:176)
at com.android.okhttp.internal.Util.discard + 158(Util.java:158)
at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.close + 435(HttpConnection.java:435)
at com.android.okhttp.okio.RealBufferedSource.close + 374(RealBufferedSource.java:374)
at com.android.okhttp.okio.RealBufferedSource$1.close + 362(RealBufferedSource.java:362)
at com.google.android.gms.internal.firebase_messaging.zzk.zza + 4(com.google.firebase:firebase-messaging@@19.0.1:4)
at com.google.firebase.messaging.zzi.close + 37(com.google.firebase:firebase-messaging@@19.0.1:37)
at com.google.firebase.messaging.zzb.zza + 51(com.google.firebase:firebase-messaging@@19.0.1:51)
at com.google.firebase.messaging.FirebaseMessagingService.zzc + 59(com.google.firebase:firebase-messaging@@19.0.1:59)
at com.google.firebase.messaging.zzg.run + 2(com.google.firebase:firebase-messaging@@19.0.1:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker + 1113(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run + 588(ThreadPoolExecutor.java:588)
at com.google.android.gms.common.util.concurrent.zza.run + 6(:6)
at java.lang.Thread.run + 818(Thread.java:818)

我在收到消息期间没有做任何网络行为,但在 Fabric 上不断收到此崩溃并且无法自己重现,这是我的 firebase 依赖项

implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.google.firebase:firebase-messaging:19.0.1'

有人遇到同样的问题吗?

-------------------------------------------- 编辑代码 fragment 在 FirebaseMessagingService 中

/* ------------------------------ Override */

override fun onMessageReceived(p0: RemoteMessage?) {
super.onMessageReceived(p0)
p0?.also { handleFcm(it.data) }
}

/* ------------------------------ Handle FCM by Type */

/**
* Handle FCM action by type
*
* @param dataMap data from FCM
*/
private fun handleFcm(dataMap: Map<String, String>) {
// Fetch data from FCM data map
val fcmModel = FcmModel(dataMap)

// GA
if (fcmModel.name.isNotEmpty()) {
GAManager.getInstance().event(
GAEventCategory.PUSH_NOTIFICATION,
GAEventAction.RECEIVE,
fcmModel.name)
}

// create intent for PendingIntent
val intent = FcmUtils.createIntent(this, fcmModel)

// create channel for Android O
FcmUtils.createChannelForAndroidO(this, fcmModel)
buildNotification(intent, fcmModel)
}

基本上,当接收到数据时,对每个字段进行解析并根据字段创建相应的 Intent ,然后创建 NotificationCompat。

-------------------------------------------- 编辑代码 fragment 在 GAManager 中

public class GAManager {

// Instance
private static GAManager GAManager;

// GA tracker
private Tracker tracker;

/* ---------------------------------- Instance & initial */

@NonNull
public static GAManager getInstance() {
if (GAManager == null) {
GAManager = new GAManager();
}
return GAManager;
}

/**
* Get tracker id from current market
*
* @param context the context
*/
public void initial(@NonNull Context context) {
// Default GA
int trackerEnvironment = getGAbyMarketSource();
tracker = GoogleAnalytics.getInstance(context).newTracker(trackerEnvironment);

// Enable Advertising Features.
tracker.enableAdvertisingIdCollection(true);
}

在应用程序onCreate时调用GAManager中的函数initial,是这个原因吗?

-------------------------------------------- 发现问题

经过实验,发现问题是由于在firebase云消息控制台上添加图像引起的,当应用程序处于后台时会随机崩溃某些设备 enter image description here

最佳答案

您使用了一个可能在另一个线程中初始化的单例类。您必须使用服务上下文获取它的实例。

GoogleAnalytics mGaInstance = GoogleAnalytics.getInstance(serviceContext);

关于android - Firebase 云消息在后台崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351908/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com