简体   繁体   中英

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

I am trying to implement push notifications on Huawei devices in this way:

Objectives

(1) When my app is in the foreground (active), I would like to have it transparently process the push messages without displaying the pop-up system notification.

(2) When my app is not started or is in the background, the standard pop-up should be displayed. Tapping on the pop-up opens up my app, passes the data in the data message along so my app can process it.

Implementation

By setting foreground_show: false, I am able to achieve (1). However, for (2), when I send a push notification from my server when my app is not active, nothing is happening on the device!

My output from logcat (verbose):


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

My code

The payload of the JSON object I am sending via a REST call to Huawei's API:

{
    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' ],
    }
}

Based on my research, this may have something to do with my AndroidManifest.xml file. In particular, I've set exported: true under my activity tag:

<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>

Question

How do I resolve the issue of the notification not displaying when my app is in the background?

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

The Android manifest file didn't include filter to take push message notification part.

Could you confirm if permission attribute (android:permission = "abcd") is added to the activity that handles the push notification in the manifest file? Please remove this attribute and try again for push notification in the background. The root cause is that notification center could not pass the message to the proper app/activity due to permission problem. Above solution is just one example.

The easiest way to get message content from Huawei Push Service is to use a Data Message instead of a Notification Message. You can send data messages from your server with key-value pairs and catch this message in onMessageReceived() method. After catching this message you can show notification by using Android Notification or you can do whatever you want.

To check if the application is running, you can define a static boolean variable in the App class and make it true when the application runs. You can decide whether to show a notification or not according to the status of this variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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