2

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?

  • Hi Joey,Could you please provide your complete log in the case of Objectives (2), so that we can analyse this issue. You can get the complete log from the following steps. The procedure for capturing logs is as follows: 1. Run the following command to connect to the device: adb logcat –v time > D:\hwpush.log 2. Reproduce the fault. 3. Press Ctrl+C to complete log capture. – zhangxaochen Jul 05 '21 at 02:06
  • Hi Shirley, I have added the full output from logcat as per your request. I am actually using Huawei's Cloud Debugging service (Huawei P40). Please let me know if you require any further information – Joey Wong Chong Ee Jul 05 '21 at 03:00

3 Answers3

0

According to the logs you provided, you are advised to check whether permission or exported = false is set on the activity to be opened in the notification bar click event.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • My project is in Flutter and there is only one activity in my project (".MainActivity"). If you take a look at my AndroidManifest.xml file (attached in the question), you can see that I've set android:exported="true" in the activity tag. By the way, what is the meaning of: action: "com.huawei.codelabpush.intent.action.test" in the JSON payload? Am I supposed to set this to my package name and create some sort of Android notification channel? I got that line from the official Huawei docs – Joey Wong Chong Ee Jul 05 '21 at 08:14
  • You can use Push Kit for [flutter plugin](https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/introduction-0000001050176002),About the second question that you asked, you can refering the [Docs](https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/deeplinking-custom-0000001055875857). – zhangxaochen Jul 05 '21 at 09:23
  • I've been using Huawei Push Kit's flutter plugin on pub.dev from day 1. Notifications are still not showing when my app is in the background. I've attempted to workaround it by implementing data messages instead. Same result: when my app is in the background, data messages aren't showing. I've successfully registered the handler in Flutter and can confirm that the OS is receiving the message but it's not displaying. Can you point me to the official docs for Flutter (Dart + Kotlin - with v2 embedding) which shows implementation of background data messages? I couldn't find any docs on this. – Joey Wong Chong Ee Jul 06 '21 at 03:23
0

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.

Zinna
  • 1,947
  • 2
  • 5
  • 20
  • The attached AndroidManifest.xml is my full file. There's no android:permission attribute in the activity tag. I have an intent-filter tag under the activity tag as you can see – Joey Wong Chong Ee Jul 24 '21 at 01:42
0

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.

Wooz12345
  • 159
  • 10