How to use nearby service to develop accurate advertising push function for nearby people

Time:2020-10-16

When you want to find a restaurant for dinner, but you don’t know which one to go to, a notice will pop up on your mobile phone and automatically push the information of the nearby high-quality restaurants for you. Will you click to check it? When you are still struggling in the store about whether to buy a pair of shoes, the mobile app gives you a 50% discount coupon for regular customers. Do you have any reason to refuse such an advertisement?

This kind of advertisement not only won’t cause the user‘s boredom, but also satisfied the user’s demand. More accurate advertising push, reduce unnecessary interruption to users, improve user satisfaction with the application. So how can I add a precise advertising push function for nearby people to my app?

How to use nearby service to develop accurate advertising push function for nearby people

It can access Huawei’s short distance communication service and realize it through Bluetooth beacon message When beacon is deployed in shopping malls, beacon messages provide accurate relative location information. When users approach restaurants and stores, they will receive pre configured promotional messages, such as coupons, discount information, and so on, so as to promote the store conveniently. The following figure shows the function.

How to use nearby service to develop accurate advertising push function for nearby people

   if you are interested in the implementation, you can download the source code from GitHub:
  https://github.com/HMS-Core/h…

1. Development preparation

If you are already a Huawei developer, you can omit this step. If you have no previous experience in integrating Huawei mobile services, you need to configure appgallery connect first, open up the short-range communication service and integrate the HMS SDK. Please refer to the official documents for the relevant steps.

1.1 add Huawei Maven warehouse and AGCP configuration in project level gradle

Add the following Maven address and AGCP configuration incrementally. Do not modify the following but copy.

buildscript {
     repositories {
         google()
         jcenter()
         maven { url 'http://developer.huawei.com/repo/' }
     }
     dependencies {
         classpath 'com.android.tools.build:gradle:3.4.1'
         classpath 'com.huawei.agconnect:agcp:1.2.1.301'
     }
 }
  
 allprojects {
     repositories {
         google()
         jcenter()
         maven { url 'http://developer.huawei.com/repo/' }
     }
 }

1.2 at the application level build.gradle Add SDK dependency

To introduce the short range communication service SDK, the most important points are as follows com.huawei SDK at the beginning.

dependencies {
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation 'androidx.appcompat:appcompat:1.1.0'
     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
     testImplementation 'junit:junit:4.12'
     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
     implementation "com.huawei.hmf:tasks:1.3.1.301"
     implementation "com.huawei.hms:network-grs:1.0.9.302"
     implementation 'com.huawei.agconnect:agconnect-core:1.2.1.301'
     implementation 'com.huawei.hms:nearby:4.0.4.300'
     api 'com.google.code.gson:gson:2.8.5'
 }

1.3 in AndroidManifest.xml File application network permissions, Bluetooth permissions and location permissions

The following permissions are shown in the name, such as android.permission.INTERNET You need network permissions, android.permission.BLUETOOTH You need Bluetooth rights. The following permissions are required.

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.BLUETOOTH" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

2. Code development

2.1 initialization and dynamic permission application

Oncreate is a method that will be called when an activity is created. In this method, we can do some preparatory actions, such as applying for necessary permissions, and checking whether mobile network, Bluetooth and GPS are enabled.

@RequiresApi(api = Build.VERSION_CODES.P)
 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     Log.i(TAG, "onCreate");
     setContentView(R.layout.activity_canteen);
     boolean isSuccess = requestPermissions(this, this);
     if (!isSuccess) {
         return;
     }
     Log.i(TAG, "requestPermissions success");
     if (!NetCheckUtil.isNetworkAvailable(this)) {
         showWarnDialog(Constant.NETWORK_ERROR);
         return;
     }
     if (!BluetoothCheckUtil.isBlueEnabled()) {
         showWarnDialog(Constant.BLUETOOTH_ERROR);
         return;
     }
     if (!GpsCheckUtil.isGpsEnabled(this)) {
         showWarnDialog(Constant.GPS_ERROR);
         return;
     }
     intView();
     init();
 }

  Register to monitor. When it is detected that the Bluetooth, GPS and network of the mobile phone are not connected, a prompt will be given:The following is the alert dialog component in Android to prompt.

private void showWarnDialog(String content) {
     DialogInterface.OnClickListener onClickListener =
         new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 android.os.Process.killProcess(android.os.Process.myPid());
             }
         };
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setTitle(R.string.warn);
     builder.setIcon(R.mipmap.warn);
     builder.setMessage(content);
     builder.setNegativeButton(getText(R.string.btn_confirm), onClickListener);
     builder.show();
 }

2.2 process of receiving beacon message

The following startscanning method is called in the OnStart method to start Bluetooth scanning. In the following message handler object, four callback methods are encapsulated (onfound indicates that the beacon message has been obtained, onlost means that the message is lost, ondistancechanged indicates the distance change between beacon and the mobile phone, and onblesignalchanged indicates that the signal change of beacon is detected (2).

The most important method is the following doonfound method, which represents the processing method of the client when receiving the beacon message, which can be displayed individually.

private void doOnFound(Message message) {
     if (message == null) {
         return;
     }
     String type = message.getType();
     if (type == null) {
         return;
     }
     String messageContent = new String(message.getContent());
     Log.d(TAG, "New Message:" + messageContent + " type:" + type);
     if (type.equalsIgnoreCase(Constant.CANTEEN)) {
         operateOnFoundCanteen(messageContent);
     } else if (type.equalsIgnoreCase(Constant.NOTICE)) {
         operateOnFoundNotice(messageContent);
     }
 }

Custom display:
   the following method is just a simple demonstration of one of the message processing methods, including the notification bar banner notification on the client, page font sliding display and other operations.

private void operateOnFoundCanteen(String messageContent) {
     CanteenAdapterInfo canteenAdapterInfo =
             (CanteenAdapterInfo) JsonUtils.json2Object(messageContent, CanteenAdapterInfo.class);
     if (canteenAdapterInfo == null) {
         return;
     }
     String canteenName = canteenAdapterInfo.getCanteenName();
     if (canteenName == null) {
         return;
     }
     Log.d(TAG, "canteenName:" + canteenName);
     if (!canteenNameList.contains(canteenName)) {
         return;
     }
  
     String notice = "";
     if (receivedNoticeMap.containsKey(canteenName)) {
         notice = receivedNoticeMap.get(canteenName);
     }
     int canteenImage = getCanteenImage(canteenName);
     int requestCode = getRequestCode(canteenName);
     canteenAdapterInfo.setNotice(notice);
     canteenAdapterInfo.setCanteenImage(canteenImage);
     canteenAdapterInfo.setShowNotice(true);
     canteenAdapterInfo.setRequestCode(requestCode);
  
     canteenAdapterInfoMap.put(canteenName, canteenAdapterInfo);
     canteenAdapterInfoList.add(canteenAdapterInfo);
  
     sendNotification(Constant.NOTIFICATION_TITLE, Constant.NOTIFICATION_SUBTITLE, canteenName, requestCode);
     runOnUiThread(
         new Runnable() {
             @Override
             public void run() {
                 searchTipTv.setText(R.string.found_tip);
                 loadingLayout.setVisibility(View.GONE);
                 canteenAdapter.setDatas(canteenAdapterInfoList);
             }
         });
 }

Conclusion

In this demo, the Bluetooth beacon message subscription function of Huawei nearby service is used

Based on the ability of nearby beacon message, it can not only be used for advertising push, but also realize the following related functions:

  1. After integrating the nearby beacon message function, the car life application can identify the user approaching the car, notify the app to enable keyless entry, record the driving track, etc.
  2. The office application integrates the nearby beacon message function, which can quickly and accurately record the employees’ clock in position.
  3. After tourism and Exhibition applications integrate the nearby beacon message function, users can get the corresponding introduction information when they are close to an exhibit or cultural relic.
  4. Game applications use nearby beacon message to realize the interaction between game and real scene. Trigger game levels through real objects, or give game props to players who participate in offline activities.

For more detailed development guidelines, please refer to the official website of Huawei developer alliance


Past links: super simple integration of ML kit to broadcast dictation words
Link to the original text: https://developer.huawei.com/consumer/cn/forum/topicview?tid=0201283747724140328&fid=18
Original author: Zhao Zhao

Recommended Today

CentOS install Chinese Fonts

preface When we encounter some Chinese display garbled problems, it may be because the server does not install Chinese fonts install 1. View system fontsBefore starting the installation, we first check the installed fonts in the system. To view the installed fonts in the system, we can use thefc-listIf there is no command in the […]