HMS-Core / hms-flutter-plugin

This repo contains all of Flutter HMS plugins.
https://developer.huawei.com/consumer/en/doc/overview/HMS-Core-Plugin?ha_source=hms1
Apache License 2.0
282 stars 139 forks source link

How to get data from push notification all data is null , and console issues #85

Closed mustafa-707 closed 3 years ago

mustafa-707 commented 3 years ago

Description

I have two question after using the plugin :

  1. After configure all settings for this plugin from the docs : https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/prepare-dev-env-0000001051136140

My Huawei console still getting Not configured as the following figure :

Screen Shot 2021-04-28 at 11 34 13 AM

But overall the notification received as local notification and as push notification successfully in foreground, background and kill state

  1. i used the api to send via post man https://push-api.cloud.huawei.com/v1/[app id]/messages:send to send push notification and this's what i tried to send

    
    {
    "validate_only": false,
    "message": {
    "data": "",
        "android": {
            "fast_app_target": 1,
            "collapse_key": -1,
            "delivery_priority": "HIGH",
            "ttl": "1448s",
            "bi_tag": "aw",
    
            "notification": {
                "title": "title t",
                "body": "body body",
                "click_action": {
                    "type": 3,
                    "intent": "#Intent;compo=com.rvr/.Activity;S.W=U;end"
                }
            }        
        },
        "token": [
            "token here"
        ]
    },
    "extras":{
      "notification":{
         "color":"white",
         "importance":"max",
         "sound":null,
         "vibrate":true,
         "title":"title title",
         "userInteraction":true,
         "soundName":null,
         "shortcutId":null,
         "vibrateDuration":1000,
         "number":null,
         "ongoing":false,
         "repeatTime":0,
         "groupSummary":false,
         "playSound":true,
         "showWhen":true,
         "smallIcon":"ic_notification",
         "fireDate":0,
         "id":1995880740,
         "tag":"aw-tag",
         "autoCancel":false,
         "channelId":null,
         "group":null,
         "ticker":null,
         "bigPictureUrl":null,
         "invokeApp":true,
         "largeIconUrl":null,
         "repeatType":null,
         "allowWhileIdle":false,
         "message":"message message",
         "subText":"subText subText",
         "bigText":"bigText bigText",
         "dontNotifyInForeground":false,
         "channelDescription":null,
         "largeIcon":"ic_launcher",
         "channelName":null,
         "actions":null
      }
    },
    "uriPage":"uri",
    "remoteMessage":{
      "data":"",
      },
      "dataOfMap":{ },
    }
    }
But what i received is this by `onNotificationOpenedApp`  :

{ "remoteMessage":{ "data":null, "originalUrgency":2, "receiptMode":0, "messageId":null, "ttl":86400, "token":null, "notification":{ "titleLocalizationArgs":null, "LightSettings":null, "icon":null, "isLocalOnly":true, "title":null, "body":null, "ChannelId":null, "Importance":null, "isAutoCancel":true, "isDefaultLight":true, "BadgeNumber":null, "vibrateConfig":null, "visibility":null, "titleLocalizationKey":null, "Color":null, "Ticker":null, "bodyLocalizationArgs":null, "ImageUrl":null, "isDefaultSound":true, "Sound":null, "bodyLocalizationKey":null, "When":null, "intentUri":null, "ClickAction":null, "Tag":null, "NotifyId":0, "Link":null, "isDefaultVibrate":true }, "dataOfMap":{

  },
  "messageType":null,
  "urgency":2,
  "contents":0,
  "collapseKey":null,
  "sentTime":0,
  "from":null,
  "to":null,
  "sendMode":0

}, "uriPage":null, "extras":{ "analyticInfo":null, "_push_data_version":1, "_hw_from":9105163812218546416, "_push_notifyid":-1558567427, "_push_msgid":-1207543745, "_push_cmd_type":"app" } }


there's any method that's get the real data that i pushed from the backend 

i was seen this useful article :
https://medium.com/huawei-developers/sending-push-notifications-on-flutter-with-huawei-push-kit-plugin-534787862b4d

which used 

static const EventChannel DataMessageEventChannel = EventChannel(Channel.DATA_MESSAGE_CHANNEL);

to get data from the native channel but with latest update of this plugin this channel is deprecated 

Also i used the example method 

@override void initState() { super.initState(); ... bool backgroundMessageHandler = await Push.registerBackgroundMessageHandler(backgroundMessageCallback); print("backgroundMessageHandler registered: $backgroundMessageHandler"); ... } ... static void backgroundMessageCallback(RemoteMessage remoteMessage) async { String data = remoteMessage.data;

Push.localNotification({
  HMSLocalNotificationAttr.TITLE: '[Headless] DataMessage Received',
  HMSLocalNotificationAttr.MESSAGE: data
});

}


and it just runs first time run and it got back as push notification 

i used `"foreground_show": false,` to handle it on foreground , but in background it received but without any indicator or data  , because `registerBackgroundMessageHandler` is not stream  how i can got the data in background ?

and i was saw other issues , while the suggestion always to use data message instead of notification message , how i can send data message with REST 
i only seend this : 
https://developer.huawei.com/consumer/en/doc/development/HMSCore-References-V5/https-send-api-0000001050986197-V5
atavci commented 3 years ago

Hello @mustafa-707,

For your first question, The "Not Configured" text probably refers to the other configurations (App Receipt, Receiving Uplink Messages etc.). Enabling the push service, and integrating as you have done by the documents in the Huawei Developers Website is enough for receiving push notifications.

For the second question. The Android notification property fields are only present for local notifications that sent with the Push Kit. In remote push notifications that sent with REST or AppGallery these fields are not available. If you need to send data within a push notification you can add Custom Key-Value Pairs or a Custom Intent URI to your notification. Also you can send a data message instead.

You can add Custom Intent URI and Custom Key-Value Pairs to your notification in AppGallery as in the picture below. image

You can reach the custom key-value pairs from the extras of the map object received in onNotificationOpenedApp or getInitialNotification and you can follow this document for obtaining the Custom Intent URIs.

The callback you provide for the registerBackgroundMessageHandler should be triggered automatically without the need of a stream. Remember that the callback should be a top level static function in order to run from the background. The remote message object that contain the data can be obtained in this callback.

For sending data messages and more with the Push REST API you can check this medium article. The article is written for native Android but the sending messages with REST API is the same for the Flutter plugin.

Hopefully these answers will clear most of the things out, I wish you a great day.

mustafa-707 commented 3 years ago

@atavci yes thank you for all this information , but my background top level static function is not working I guess because I'm using the flutter V2 configuration , also when app is killed the app not get any data even if it a Custom Intent URI , So overall no problem i used intent URL to send data and all good in foreground and background and kill except the mentioned issues , Thank you