gpt4 book ai didi

Android FCM 通知不会推送 API 25-

转载 作者:行者123 更新时间:2023-11-29 16:50:37 25 4
gpt4 key购买 nike

我正在尝试将 firebase 云消息传递添加到我的项目中。问题是我想在设备处于 sleep 状态或应用程序处于后台时推送通知。 Firebase 文档说,当您在应用程序不在前台时发送通知时,系统会完成这项工作,但为什么它适用于 API 26 而不是 API 25 更低的版本?

前台应用程序中的通知由我处理并且工作正常,我的目标 API 是 26 - 编译的 SDK 26.0.2 并使用最新版本的 FCM pkg。

我的测试设备是带有 API 19、22、25、26 的 adbs和带有 API 25 的 Xperia X,三星 S8 API 26

我发送的通知 JSON:

    {
"to" : "existing key",
"condition" : null,
"collapse_key" : null,
"priority" : "high",
"content_available" : null,
"time_to_live" : 3600,
"restricted_package_name" : null,
"dry_run" : null,
"data" : null,
"registration_ids" : null,
"notification" : {
"title" : "ViLo Testzor",
"body" : "Alarm. ",
"sound" : null,
"icon" : null,
"tag" : null,
"color" : null,
"badge" : null,
"bodyLocKey" : null,
"bodyLockArgs" : null,
"clickAction" : null,
"titleLocKey" : null,
"titleLocArgs" : null
}
}

我的 list :

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="cz.pkg.app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />


<application
android:name=".android.App"
android:allowBackup="true"
android:debuggable="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:minSdkVersion="19"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppFullScreenTheme">
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_app_icon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel"
android:value="@string/default_notification_channel_id" />
<activity
android:name=".android.ActivityMain"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/AppFullScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".android.SenzasenFirebaseInstanceIdService"
android:enabled="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".android.SenzasenMessagingService"
android:enabled="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".android.BackgroundWatcher"
android:enabled="true"
android:exported="true" />

<receiver
android:name=".android.notifications.BroadcastRecieverUpdater"
android:enabled="true"
android:exported="true"></receiver>
</application>

</manifest>

我的 gradle 文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "cz.pkg.app"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}


compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}


dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-v4:26.0.0-alpha1'
compile 'com.google.android.gms:play-services-maps:11.4.2'
compile 'com.google.firebase:firebase-core:11.4.2'
compile 'com.google.firebase:firebase-messaging:11.4.2'
testCompile 'junit:junit:4.12'
}


apply plugin: 'com.google.gms.google-services'

这是发送通知时的 Firebase 日志:

    10-05 12:25:04.444 5568-5568/? I/FA: App measurement is starting up, version: 11400
10-05 12:25:04.444 5568-5568/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
10-05 12:25:04.450 5568-5568/? V/FA: Collection enabled
10-05 12:25:04.450 5568-5568/? V/FA: App package, google app id: cz.pkg.app, 1:535056224042:android:2977b77751aa5b1f
10-05 12:25:04.451 5568-5568/? I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app cz.pkg.app
10-05 12:25:04.451 5568-5568/? D/FA: Debug-level message logging enabled
10-05 12:25:04.455 5568-5568/? V/FA: Cancelling job. JobID: 717907879
10-05 12:25:04.456 5568-5568/? V/FA: Registered activity lifecycle callback
10-05 12:25:04.464 5568-5590/? V/FA: Using measurement service
10-05 12:25:04.464 5568-5590/? V/FA: Connecting to remote service
10-05 12:25:04.471 5568-5590/? V/FA: Using measurement service
10-05 12:25:04.471 5568-5590/? V/FA: Connection attempt already in progress
10-05 12:25:04.475 5568-5568/? V/FA: onActivityCreated
10-05 12:25:04.527 5568-5590/cz.pkg.app V/FA: Using measurement service
10-05 12:25:04.527 5568-5590/cz.pkg.app V/FA: Connection attempt already in progress
10-05 12:25:04.529 5568-5590/cz.pkg.app V/FA: Activity resumed, time: 57001950
10-05 12:25:04.533 5568-5590/cz.pkg.app I/FA: Tag Manager is not found and thus will not be used
10-05 12:25:04.534 5568-5590/cz.pkg.app D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]
10-05 12:25:04.547 5568-5590/cz.pkg.app V/FA: Using measurement service
10-05 12:25:04.547 5568-5590/cz.pkg.app V/FA: Connection attempt already in progress
10-05 12:25:04.599 5568-5590/cz.pkg.app D/FA: Connected to remote service
10-05 12:25:04.599 5568-5590/cz.pkg.app V/FA: Processing queued up service tasks: 4
10-05 12:25:04.618 7477-5602/? V/FA-SVC: Logging event: origin=auto,name=screen_view(_vs),params=Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]
10-05 12:25:04.621 7477-5602/? V/FA-SVC: Saving event, name, data size: screen_view(_vs), 57
10-05 12:25:04.622 7477-5602/? V/FA-SVC: Event recorded: Event{appId='cz.pkg.app', name='screen_view(_vs)', params=Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]}
10-05 12:25:04.623 7477-5602/? V/FA-SVC: Upload scheduled in approximately ms: 1458892
10-05 12:25:04.625 7477-5602/? V/FA-SVC: Cancelling job. JobID: 812057698
10-05 12:25:04.625 7477-5602/? V/FA-SVC: Scheduling upload with AlarmManager
10-05 12:25:04.626 7477-5602/? V/FA-SVC: Background event processing time, ms: 8
10-05 12:25:08.146 5568-5590/cz.pkg.app V/FA: Recording user engagement, ms: 3619
10-05 12:25:08.148 5568-5590/cz.pkg.app V/FA: Activity paused, time: 57005569
10-05 12:25:08.152 5568-5590/cz.pkg.app D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=3619, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]
10-05 12:25:08.205 7477-5602/? V/FA-SVC: Logging event: origin=auto,name=user_engagement(_e),params=Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=3619, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]
10-05 12:25:08.210 7477-5602/? V/FA-SVC: Saving event, name, data size: user_engagement(_e), 67
10-05 12:25:08.210 7477-5602/? V/FA-SVC: Event recorded: Event{appId='cz.pkg.app', name='user_engagement(_e)', params=Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=3619, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]}
10-05 12:25:08.212 7477-5602/? V/FA-SVC: Upload scheduled in approximately ms: 1455303
10-05 12:25:08.213 7477-5602/? V/FA-SVC: Cancelling job. JobID: 812057698
10-05 12:25:08.214 7477-5602/? V/FA-SVC: Scheduling upload with AlarmManager
10-05 12:25:08.214 7477-5602/? V/FA-SVC: Background event processing time, ms: 9
10-05 12:25:15.012 5707-5707/? I/FA: App measurement is starting up, version: 11400
10-05 12:25:15.012 5707-5707/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
10-05 12:25:15.022 5707-5707/? V/FA: Collection enabled
10-05 12:25:15.022 5707-5707/? V/FA: App package, google app id: cz.pkg.app, 1:535056224042:android:2977b77751aa5b1f
10-05 12:25:15.024 5707-5707/? I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app cz.pkg.app
10-05 12:25:15.024 5707-5707/? D/FA: Debug-level message logging enabled
10-05 12:25:15.033 5707-5707/? V/FA: Cancelling job. JobID: 717907879
10-05 12:25:15.039 5707-5707/? V/FA: Registered activity lifecycle callback
10-05 12:25:15.059 5707-5726/? V/FA: Using measurement service
10-05 12:25:15.061 5707-5726/? V/FA: Connecting to remote service
10-05 12:25:15.074 5707-5726/? V/FA: Using measurement service
10-05 12:25:15.074 5707-5726/? V/FA: Connection attempt already in progress
10-05 12:25:15.078 5707-5726/? D/FA: Connected to remote service
10-05 12:25:15.079 5707-5726/? V/FA: Processing queued up service tasks: 2
10-05 12:25:20.114 5707-5726/cz.pkg.app V/FA: Inactivity, disconnecting from the service

非常感谢 Radek 的每一个回复。

最佳答案

在您的情况下,如果您的应用程序处于后台或处于终止状态,Firebase 将不会调用 onMessageReceived()

要在后台或被杀死状态下获取通知,请使用data 对象而不是notification 对象。

以下将起作用:

{
"to" : "existing key",
"condition" : null,
"collapse_key" : null,
"priority" : "high",
"content_available" : null,
"time_to_live" : 3600,
"restricted_package_name" : null,
"dry_run" : null,
"data" : {
"title" : "ViLo Testzor",
"body" : "Alarm. ",
"sound" : null,
"icon" : null,
"tag" : null,
"color" : null,
"badge" : null,
"bodyLocKey" : null,
"bodyLockArgs" : null,
"clickAction" : null,
"titleLocKey" : null,
"titleLocArgs" : null
},
"registration_ids" : null,
"notification" :null
}

关于Android FCM 通知不会推送 API 25-,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46583602/

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