繁体   English   中英

当 foreground_show 设置为 false 并且我的应用程序在后台时,如何获取推送通知以显示?

[英]How do I get push notifications to show when foreground_show is set to false and my app is in the background?

我正在尝试以这种方式在华为设备上实现推送通知:

目标

(1) 当我的应用程序在前台(活动)时,我希望它透明地处理推送消息而不显示弹出系统通知。

(2) 当我的应用程序未启动或在后台时,应显示标准弹出窗口。 点击弹出窗口打开我的应用程序,传递数据消息中的数据,以便我的应用程序可以处理它。

执行

通过设置foreground_show: false,我能够实现(1)。 但是,对于 (2),当我的应用程序未处于活动状态时从我的服务器发送推送通知时,设备上什么也没有发生!

我的 logcat 输出(详细):


2021-07-05 10:48:02 30209-1281/? W/AwareLog: AwareMem_Reclaim: getPssForPid error skip! procName=com.huawei.hwid.core
2021-07-05 10:48:02 923-953/? D/: HibSTrategy::HibStrategySwapCandidateProcessAdd packageName com.huawei.android.pushagent
2021-07-05 10:48:02 30898-6851/? I/PGServer: getTopFrontApp. calling pkg: android
2021-07-05 10:48:02 10315-10315/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:02 10315-10315/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:02 923-3399/? D/AwareLog: HiberManagerService::DoReclaim ok, pid=2351, reclaimMode=hiber_anon
2021-07-05 10:48:02 923-3399/? D/AwareLog: GetReclaimResult:reclaimKb=0, swapKb=0, swapUs=574
2021-07-05 10:48:02 923-3399/? D/: HibStrategy::HibStrategySwapCandidateProcessRemove packageName is com.huawei.android.pushagent
2021-07-05 10:48:02 923-3399/? D/AwareLog: HiberManagerService::SendMessageToHiberTask successful
2021-07-05 10:48:02 10313-10313/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:02 10313-10313/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:02 31536-32767/? I/PushLog110102300: [SocketRead_17:39:13-72]enter cancelAlarm(Action=com.huawei.android.push.intent.RESPONSE_FAIL
2021-07-05 10:48:02 31536-32767/? I/PushLog110102300: [SocketRead_17:39:13-72]process cmdid to receive from pushSrv:44, subCmdId:FF
2021-07-05 10:48:02 31536-32767/? I/PushLog110102300: [SocketRead_17:39:13-72]dispatchIntent over
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50]process push message cmdid from pushSrv:44, subCmdId:FF
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50]msgType: 2 [0:PassBy msg, 1:System notification, 2:normal notification, 3:HCM PassBy msg]
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50] closeTokenCheck: false
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50]custom channelId is empty
2021-07-05 10:48:02 31536-31992/? E/SecurityComp10105302: kj: content or algorithm is null.
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50]accountVerify profileId is empty.
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50]app declare HmsMessageService num is: 1
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50]no running process.
2021-07-05 10:48:02 31536-31992/? E/PushLog110102300: [ReceiverDispatcher-50]no permission to start activity, appPackageName:my.packageName, isUri:false
2021-07-05 10:48:02 31536-31992/? E/PushLog110102300: [ReceiverDispatcher-50]no permission to start activity:my.packageName,msgID:0E61558047A96124
2021-07-05 10:48:02 31536-31992/? E/PushLog110102300: [ReceiverDispatcher-50]NC message is invalid, can not display.
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50]process cmdid to send to pushSrv:45, subCmdId:FF
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50]send msg to remote srv success
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50]handleMessageResponse the response msg is :45,msgId:0E61558047A96124,displayPkgName:my.packageName,flag:94
2021-07-05 10:48:02 31536-31992/? I/PushLog110102300: [ReceiverDispatcher-50]enter AlarmTools:setInexactAlarm(intent:Intent { act=com.huawei.action.push.intent.CHECK_CHANNEL_CYCLE pkg=android } interval:1200000ms
2021-07-05 10:48:02 30209-3902/? I/AlarmManager: remove alarm:Alarm{f82f7b7 type 1 when 1625454447206 PendingIntent{b60bf97: PendingIntentRecord{d9d6467 android broadcastIntent}}android procName com.huawei.android.pushagent.PushService} according to operation:9c75eb6
2021-07-05 10:48:02 30209-30237/? I/AppEyeFwkBlock: do monitor
2021-07-05 10:48:02 10324-10324/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:02 10324-10324/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:02 10329-10329/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:02 10329-10329/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:02 711-711/? I/SurfaceFlinger: doPartialComposeImpl: reject partial compose request.
2021-07-05 10:48:02 711-711/? I/SurfaceFlinger: doPartialComposeImpl: reject partial compose request.
2021-07-05 10:48:03 10337-10337/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:03 10337-10337/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:03 10336-10336/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:03 10336-10336/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:03 30209-30542/? I/TrafficMonitor: expired arrive. level:-1
2021-07-05 10:48:03 30209-30542/? I/TrafficMonitor: during_ms:2001,period_ms:2000
2021-07-05 10:48:03 30209-30542/? I/TrafficMonitor: count:330,rx_sum:0,tx_sum:0,rxBytes:0,during_ms:2001,rx_speed:0.0,tx_speed:0.0,rto:0.0
2021-07-05 10:48:03 30209-30542/? I/HuaweiWifiWatchdogStateMachine: Get speed information rx_speed = 0 ,tx_speed = 0 ,isSpeedOk = false ,mBestSpeedInPeriod = 0
2021-07-05 10:48:03 30713-6875/? E/HwNaturalBase: [Binder:30713_8]: Failed to check permission, check database/table permission failed.
2021-07-05 10:48:03 30713-6875/? E/HwNaturalBase: [Binder:30713_8]: dsCollectEncrypt-0: Failed to registerListener, check database/table permission failed.
2021-07-05 10:48:03 27863-28586/? E/HwNaturalBaseClient: Failed to register observer for RawFgAPPEvent.
2021-07-05 10:48:03 27863-28586/? E/HwNaturalBaseClient: Failed to register remote observer.
2021-07-05 10:48:03 27863-28586/? E/OdmfHelper: CAWARENESS: subscribeManagedObject isSuccess = false
2021-07-05 10:48:03 10344-10344/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:03 10344-10344/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:03 10349-10349/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:03 10349-10349/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:03 10354-10354/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:03 10354-10354/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:03 923-953/? W/: Failed to SetQos, ret=-1, tid=10359, qos=1
2021-07-05 10:48:03 923-953/? E/AwareLog: iawared: GetFileContent open /proc/10354/task/10360/cpuset fail errno(2)!
2021-07-05 10:48:03 923-953/? W/: Failed to SetQos, ret=-1, tid=10360, qos=1
2021-07-05 10:48:03 10357-10357/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:03 10357-10357/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:03 10364-10364/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:03 10364-10364/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:03 10369-10369/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:03 10369-10369/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:04 711-711/? I/SurfaceFlinger: doPartialComposeImpl: reject partial compose request.
2021-07-05 10:48:04 711-711/? I/SurfaceFlinger: doPartialComposeImpl: reject partial compose request.
2021-07-05 10:48:04 10377-10377/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:04 10377-10377/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:04 10376-10376/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:04 10376-10376/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:04 10384-10384/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:04 10384-10384/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:04 10389-10389/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:04 10389-10389/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:04 705-705/? E/WifiHAL: enter wifi_get_link_stats
2021-07-05 10:48:04 30209-30543/? E/WificondControl: Noise: -94, Snr: 127, Chload: 55
2021-07-05 10:48:04 30209-30543/? V/HwWifiServiceFactory: get AllImpl object = com.android.server.wifi.HwWifiServiceFactoryImpl@212f9be
2021-07-05 10:48:04 10394-10394/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:04 10394-10394/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:04 10397-10397/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:04 10397-10397/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:04 711-711/? I/SurfaceFlinger: doPartialComposeImpl: reject partial compose request.
2021-07-05 10:48:04 10404-10404/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:04 10404-10404/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:04 30707-31174/? I/SpeedTestNotifier: is low speed scene
2021-07-05 10:48:04 10409-10409/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:04 10409-10409/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:05 10414-10414/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:05 10414-10414/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:05 10419-10419/? I/HiTraceC: entered LogRegisterGetIdFun
2021-07-05 10:48:05 10419-10419/? I/HiTraceC: entered HiTraceInit
2021-07-05 10:48:05 30713-6875/? E/HwNaturalBase: [Binder:30713_8]: Failed to check permission, check database/table permission failed.
2021-07-05 10:48:05 30713-6875/? E/HwNaturalBase: [Binder:30713_8]: dsCollectEncrypt-0: Failed to registerListener, check database/table permission failed.
2021-07-05 10:48:05 27863-28586/? E/HwNaturalBaseClient: Failed to register observer for RawFgAPPEvent.
2021-07-05 10:48:05 27863-28586/? E/HwNaturalBaseClient: Failed to register remote observer.
2021-07-05 10:48:05 27863-28586/? E/OdmfHelper: CAWARENESS: subscribeManagedObject isSuccess = false
2021-07-05 10:48:05 10424-10424/? I/HiTraceC: entered LogRegisterGetIdFun

我的代码

我通过 REST 调用发送到华为 API 的 JSON 对象的有效负载:

{
    validate_only: false,
    message: {
        android: {
            notification: {
                foreground_show: false,
                title: "My title",
                body: "Message body",
                click_action: {
                    // 1 = opens a custom app page. foreground_show: false requires this!
                    type: 1,
                    action: "com.huawei.codelabpush.intent.action.test",
                }
            }
        },
        token: [ 'myDeviceTokenHere' ],
    }
}

根据我的研究,这可能与我的 AndroidManifest.xml 文件有关。 特别是,我在活动标签下设置了导出:true

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.packageName">
    <!-- Required to fetch data from the internet -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
    <uses-permission android:name="com.android.vending.BILLING" />

    <!-- For QR scanner -->
    <uses-permission android:name="android.permission.CAMERA" />

   <application
        android:name="androidx.multidex.MultiDexApplication"
        android:requestLegacyExternalStorage="true"
        android:allowBackup="false"
        android:fullBackupContent="false"
        android:label="packageName"
        android:icon="@mipmap/launcher_icon">

        <!-- Firebase messaging -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_stat_notification_icon" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/ic_stat_notification_icon" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/notification_channel_id" />
        

        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
        </activity>

        <!-- Facebook Login configuration -->
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
        <activity android:name="com.facebook.FacebookActivity" android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:label="@string/app_name" />
        
        <activity android:name="com.facebook.CustomTabActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="@string/fb_login_protocol_scheme" />
            </intent-filter>
        </activity>

        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

问题

如何解决我的应用程序在后台时不显示通知的问题?

根据您提供的日志,建议您检查通知栏点击事件中要打开的activity是否设置了permission或exported = false

Android 清单文件不包含过滤器以获取推送消息通知部分。

您能否确认是否在处理清单文件中的推送通知的活动中添加了权限属性(android:permission = "abcd")? 请删除此属性并在后台重试推送通知。 根本原因是由于权限问题,通知中心无法将消息传递给适当的应用程序/活动。 上述解决方案只是一个例子。

从华为推送服务获取消息内容的最简单方法是使用数据消息而不是通知消息。 您可以使用键值对从服务器发送数据消息,并在 onMessageReceived() 方法中捕获此消息。 捕获此消息后,您可以使用 Android 通知显示通知,或者您可以做任何您想做的事情。

要检查应用程序是否正在运行,您可以在 App 类中定义一个静态布尔变量,并在应用程序运行时使其为真。 您可以根据该变量的状态决定是否显示通知。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM