How to identify requests from quick apps on an HTML5 page - Huawei Developers

How to identify requests from quick apps on an HTML5 page so that the service logic will not instruct users to download an app?
The web component of a quick app uses the same standard HTML execution environment as browsers such as Google Chrome and Safari. When an HTML5 web page is loaded, the User-Agent attribute is reported to the server. The HTML5 web page can obtain the User-Agent object of the current execution environment through the JavaScript function. Therefore, you can set the User-Agent attribute for the web component to identity requests from quick apps.
The implementation procedure is as follows:
1. In the quick app, set the User-Agent attribute of the web component to default.
Code:
<web id='web' src="{{websrc}}" allowthirdpartycookies="true" useragent="default"></web>
2. On the HTML5 page, check whether the window.navigator.userAgent object contains the hap keyword. If yes, the request is from a quick app.
Code:
if (window.navigator.userAgent.indexOf("hap") >= 0) {
// The request comes from a quick app.
}
For details about Huawei developers and HMS, visit the website.
https://forums.developer.huawei.com/forumPortal/en/home?fid=0101246461018590361

Related

Android using native and java activity in the same app

Is it possible to have mix of native and regular activity in the same app? I have a scenario where some GUI handling needs to be done with a c++ native library.
Android documentation "Sample: native-activity" shows how to write a full native activity but how do i add it to an existing android app. If its possible how to invoke the native activity ?
I followed the steps in the documentation and added a new native activity (everything except not as main activity) but couldnt find a way launch it from java as the intent is looking for a class object.

Standard deep links format

Multiple entries to a quick app released on AppGallery help improve the quick app's impressions and bring more traffic to developers. Standard deep links are provided to implement this function.
Deep links can be applied in the following scenarios:
l Sharing on social platforms
The deep link of a quick app shared through social platforms can redirect a user to the quick app after the user taps the deep link.
l Advertising
The deep link of a quick app embedded in an ad can redirect a user to the quick app after the user taps the deep link.
l Operational events
The deep link of a quick app embedded in the promotion page of an operational event can make it easier for users to participate in the event.
The following formats are supported:
(Recommended) hap://app/<package>/[path][?key=value]
https://hapjs.org/app/<package>/[path][?key=value]
(Applicable to Huawei phones only) hwfastapp://<package>/[path][?key=value]
l package: App package name, which is mandatory.
l path: Path of the page in the app. This parameter is optional. If this parameter is not set, the home page is used by default.
l key-value: Parameter to be passed to the page. This parameter is optional. Multiple key-value pairs are allowed. The passed parameter values may be obtained by other apps. Therefore, you are advised not to transfer data with high security sensitivity.
The following takes a demo quick app with a package name of com.huawei.quickapp as an example to describe how to use deep links.
The following information is obtained from the manifest.json file of the demo quick app.
l package: com.huawei.quickapp
l name: demo
l pages: include the Hello page and the Detail page, with Hello being the home page.
The sample code for calling the deep link to open the demo quick app is as follows:
import router from '@system.router'
router.push({
uri: 'hap://app/com.huawei.quickapp'
})
The sample code for opening the demo quick app after a user taps a link on a web page is as follows:
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="hap://app/com.huawei.quickapp" > redirect to a demo quick app</a>
</body>
</html>
In the preceding example, the page to which a user is redirected is not configured. Therefore, the Hello page of the demo quick app is displayed by default. To display the Detail page, modify the code as follows:
import router from '@system.router'
router.push({
uri: 'hap://app/com.huawei.quickapp/Detail'
})
If parameters need to be transferred to the Detail page, add the parameters to the code in the key-value format. Multiple parameters are separated by ampersands (&). If the source and name parameters are transferred to the Detail page, the code is modified as follows:
import router from '@system.router'
router.push({
uri: 'hap://app/com.huawei.quickapp/Detail?source=net&name=aa'
})
The code for obtaining the values of the parameters that have been transferred to the Detail page is as follows:
onShow: function () {
var nameValue=this.name;
var sourceValue=this.source;
}
You can test the redirection of the deep link after the development is complete.
Notice: Before the test, ensure that usage records of the quick app to be redirected to exist in Quick App Loader.
[Test Method 1] Run the android adb command. (The ADB environment must be configured on your computer.)
adb shell am start -a android.intent.action.VIEW -d hap://app/com.huawei.quickapp/Detail
Notice: This method does not apply to scenarios where multiple parameters are transferred.
[Test Method 2] Add the deep link to be tested to a local web page.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
window.location.href="hap://app/com.huawei.quickapp/Detail?source=net&name=aa";
</script>
</body>
</html>

How Can I Integrate HUAWEI Ads into a Huawei HTML5 Quick Game?

Symptom:
Currently, no ad API is provided for an HTML5 quick game. How can I integrate HUAWEI Ads into my quick game?
Analysis:
Currently, HUAWEI Ads supports only quick apps and runtime quick games, but not HTML5 quick games. You can use a two-way communication mechanism between the web component on the UX page of a quick game and the HTML5 web page of the game to integrate HUAWEI Ads into an HTML5 quick game. The onMessage lifecycle function on the UX page receives messages from an HTML5 page, calls the ad API of quick apps to obtain ad information (only available for native ads and rewarded video ads), and sends the obtained ad information to the HTML5 page through this.$element(‘web’).postMessage({ message: JSON.stringify(result) });.
Solution:
It is recommended that the ad creation and ad API request processes be separately encapsulated in different functions, instead together in the onInit or onMessage lifecycle function. This is because the onInit function is called during page initialization, which implements quicker loading, but it is not suitable for processing complex logic; the onMessage function is used to receive character strings passed by HTML5 pages. After you add a judgment branch, the corresponding function is called to perform specific ad processing.
Note: Currently, the quick app framework supports the creation of ad objects only in the onInit function, but not in functions such as onMessage. Keep the position of the code block unchanged.
For more sample code, you can refer to this site.
https://gist.github.com/Mayism/0a9f12438da0e86f06594e01e7643895
For more FAQs and cases about HUAWEI Ads integration, visit the following link:
https://developer.huawei.com/consumer/en/doc/development/quickApp-Guides/quickapp-access-ads-kit

Common Redirection Functions on HUAWEI AppGallery

With the unveiling of so many new functions and pages on HUAWEI AppGallery, it’s required a wide range of new redirection solutions on the platform.
However, the types of links, functions, apps, and scenarios that they are used, can vary great. It can be quite confusing sorting this all out, so I’ve outlined some common redirection scenarios for your reference.
Feel free to correct me, if you think that I’ve got something wrong, or am missing something.
1. Redirecting to the AppGallery Home Page
Usage case: You want to redirect the user from an app to the AppGallery home page, so that the user can search for related apps or activities quickly.
Method: Use the action method from Intent to implement the function.
action: com.huawei.appmarket.intent.action.MainActivity
Example:
Java:
public void launchAGHomePage() {
Intent intent = new Intent("com.huawei.appmarket.intent.action.MainActivity");
startActivity(intent);
}
2. Redirecting to an App’s Details Page on AppGallery
2.1 Intent-based Redirection from an In-App Page
Usage case: You want to redirect the user from an in-app page to the app’s details page, so the user can rate or comment on the app.
Methods: Use the action method from Intent to implement the function.
1. Method 1: by app ID
action:com.huawei.appmarket.appmarket.intent.action.AppDetail. withid
setPackage("com.huawei.appmarket");
name: "appId", value: "C100170981"
2. Method 2: by package name
action: com.huawei.appmarket.intent.action.AppDetail
setPackage("com.huawei.appmarket");
name: "APP_PACKAGENAME", value: "com.huawei.browser"
Note: Compared with method 2, method 1 includes additional appmarket and withid parameters from the action method.
Parameters involved:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Examples:
Method 1:by app ID
Java:
public void launchAppDetilPage1() {
Intent intent = new Intent("com.huawei.appmarket.appmarket.intent.action.AppDetail.withid");
intent.setPackage("com.huawei.appmarket");
intent.putExtra("appId", "C100170981");
startActivity(intent);
}
Method 2:by package name
Java:
public void launchAppDetilPage2() {
Intent intent = new Intent("com.huawei.appmarket.intent.action.AppDetail");
intent.setPackage("com.huawei.appmarket");
intent.putExtra("APP_PACKAGENAME", "com.huawei.browser");
startActivity(intent);
}
2.2 URL-based Redirection
Usage case: You want to redirect a user who clicks on a shared URL to an app’s details page.
Method: Create a URL with the following format:
hiapplink://com.huawei.appmarket?appId=yourAppID&channelId=yourChannelId&referrer=yourReferrer
Note: You’ll need to change the strings in either italic or bold only.
Parameters involved:
Example:
By app id
Java:
public void launchAppDetilWithURL1() {
String text1 = "hiapplink://com.huawei.appmarket?appId=C100170981&channelId=HwBrowserSearch&referrer=Keywords";
Uri uri = Uri.parse(text1);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
3. Launching All App Stores Installed on a Device to Redirect Users to an App’s Details Page in an App Store via MARKET
Usage case: You want to launch all app stores installed on the user’s device by passing a package name or app ID, so that the user can choose to go to the app’s details page in a desired app store.
Methods: Pass the link whose scheme is market://. Android supports the standard MARKET protocol, to ensure that all app stores can be launched on Android devices. The methods are as follows:
Method 1: market://details?id=pkgName // for all stores
Method 2: appmarket://details?id=pkgName // only for AppGallery
Method 3: market://com.huawei.appmarket.applink?appId=App ID" // only for AppGallery
Note: Method 1 is a standard method for Android devices, and is applicable to all app stores, such as Google Play and Tecent Appstore.
Parameters involved:
Examples:
// Method 1
Java:
public void launchAppDetilOnMarket1() {
String text1 = "market://details?id=com.huawei.browser";
Uri uri = Uri.parse(text1);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
// Method 2
Java:
public void launchAppDetilOnMarket2() {
String text1 = "appmarket://details?id=com.huawei.browser";
Uri uri = Uri.parse(text1);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
// Method 3
Java:
public void launchAppDetilOnMarket3() {
String text1 = "market://com.huawei.appmarket.applink?appId=C100170981";
Uri uri = Uri.parse(text1);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
4. Redirecting from a Web Link to an App’s Detail Page on the Web Version of AppGallery
Usage case: You want to redirect the user to an app’s details page on the web version of AppGallery, when the user clicks on a web link from the app’s official website, or a promotional web page.
Methods:
Method 1:https://appgallery.huawei.com/#/app/YOUR_APPID
Method 2::https://appgallery.cloud.huawei.com/appDetail?pkgName=pkgName
Method 3:https://appgallery.huawei.com/#/app/YOUR_APPID?pkgName=pkgName
Method 4:https://appgallery.cloud.huawei.com/marketshare/app/ YOUR_APPID?locale=LOCALE&shareTo=WAP&shareFrom=channeID
Parameters involved:
Examples:
// Method 1: by app ID
https://appgallery.huawei.com/#/app/C100170981
// Method 2: by package name
https://appgallery.cloud.huawei.com/appDetail?pkgName=com.huawei.browser
// Method 3: by app ID and package name
https://appgallery.huawei.com/#/app/C100170981?pkgName=com.huawei.browser
// Method 4: by complete link with optional parameters (not commonly used, and generally used for badge-based promotions).
HUAWEI Browser
Huawei AppGallery
appgallery.cloud.huawei.com
5. Redirecting from a Badge Link to the App Detail Page on AppGallery
When a badge link of AppGallery appears as an image of AppGallery, by clicking this link, the user will be redirected to an app’s details page on AppGallery. If you hope to market your app, you can use this badge directly. In essence, it still functions as a link, no different than any other web link.
Use cases: If you have developed and released an app, and want to divert traffic from your website or another source to AppGallery, a badge can be ideal.
How to make a badge: The method for creating a badge is rather simple. You only need to sign in to AppGallery Connect, and click AppGallery Download. Then you proceed to make the badge as prompted.
Restrictions: You can only make a badge for apps that have been released, with a maximum of one badge for each app. Once a badge has been created, you can query for it on the Search badge tab page.
Badge use instructions:
You can find a created badge on the Search badge tab page, and download the badge or copy its link. I’ll walk you through some common operations:
Badge download: You will obtain a PNG image, which can be displayed on your website, or on a marketing HTML5 page.
Link creation: You can create a link for a specific channel, for example, Facebook or Baidu.
Link copying: You can download a link for a specific channel.
Usage example:
// 1. Typical link
https://appgallery.huawei.com/#/app...EDCCBCD90A86F29A8DA2400AA4163&detailType=0&v=
// 2. Typical badge with a link, which can be clicked
6. App Linking, with Cross-Platform Link Support
App Linking is a new service provided by AppGallery Connect. Since it’s new, I’ll give you a brief overview of what it is.
The service provides links that can work on a diverse range of platforms, such as Android, iOS, and PC browsers, making it similar in this way to Dynamic Links of Firebase, enabling you to quickly build cross-platform links, which can be easily shared.
In what scenarios can I use App Linking? I’ll give you an example to help illustrate how the service can be used. It’s an app that’s available on both Android and iOS devices, and thus requires a promotional activity that reaches users on both platforms. To engage users, we’ll need to send them an activity invitation link that works on Android and iOS alike. Also, some users may be opening the link in a PC browser, and in this case, that link should also support an HTML5 page for the activity.
What benefits does App Linking offer? Here, two scenarios should be considered:
The user’s device has already installed the app: App Linking will automatically open the app, and show the user the target page.
The user’s device has not installed the app: The link will prompt the user to open the app’s details page on AppGallery, or any other app store (configurable), and download the app. Then, when the user opens the downloaded app, the target page will appear.
How does App Linking work? Links of App Linking can be created in any of the following ways:
Creation on the console: All operations are performed in AppGallery Connect. You’ll need to click My projects, click your project, and go to Grow > App Linking. To create a link of App Linking, create a URL prefix first.This mode is recommended if you’re not familiar with coding. A step will require you to provide a deep link, which will need to be obtained from your R&D personnel.
Creation in your iOS app: This mode is similar to the previous one.
The only difference is that these links are for iOS users.
What if the user’s device is not a Huawei device?
Since App Linking supports both Android and iOS, many of you may be wondering how this service works on non-Huawei Android devices.
1. Can App Linking work for non-Huawei devices? You can be rest assured App Linking is not dependent on HMS Core, and thus is supported on all Android devices, regardless of whether they are running GMS or HMS.
2. What if the user has not installed the app and AppGallery? For Android devices that don’t have AppGallery installed, you can choose to open the link in the local app store that is installed. As long as your package name on that app store is the same as that on AppGallery, the user will be able to open your app’s details page on that app store.
Usage example:
// 1. Typical URL prefix
https://photoplaza.drcn.agconnect.link // In the prefix, photoplaza is unique to the app, and drcn.agconnect.link is a fixed.
// 2. Typical link of App Linking
https://photoplaza.drcn.agconnect.link/vm3Y
// 3. Code for creating a typical link of App Linking for Android
Java:
private static final String DOMAIN_URI_PREFIX = "https://photoplaza.drcn.agconnect.link";private static final String DEEP_LINK = "https://developer.huawei.com";public void createAppLinking() {
AppLinking.Builder builder = new AppLinking.Builder()
.setUriPrefix(DOMAIN_URI_PREFIX)
.setDeepLink(Uri.parse(DEEP_LINK))
.setAndroidLinkInfo(new AppLinking.AndroidLinkInfo.Builder().build());
String LongAppLinking = builder.buildAppLinking().getUri().toString();
}
// 4. Typical link of App Linking for iOS
Objective-C:
- (IBAction)CreatLink:(id)sender {
AGCAppLinkingComponents *component = [[AGCAppLinkingComponents alloc] init];
component.uriPrefix = @"https://photoplaza.drcn.agconnect.link";
component.deepLink = @"https://www.developer.huawei.com";
component.iosBundleId = @"com.lucky.agc.demo";
component.iosDeepLink = @"agckit://ios/detail"; self.longlink.text = component.buildLongLink.absoluteString;
}
7. For Reference
Badge link document: https://developer.huawei.com/consum...ct-Guides/appgallery-agd-introduction-stamped
App Linking document: https://developer.huawei.com/consum.../agc-applinking-introduction-0000001054143215
Guide for adding a referrer: https://developer.huawei.com/consum...connect-Guides/appgallery-referrer-createlink
Guide for querying attribution information: https://developer.huawei.com/consum...ry-connect-Guides/appgallery-referrer-develop

How to Target Ads Precisely While Protecting User Privacy

Background​
When using an app, if pop-up ads keep appearing when we browse app pages but we are not interested in the advertised content, not only will our browsing experience be negatively affected, but we will also quickly become tired of the advertised content. Unwanted ads are usually annoying. Aimless ad targeting and delivery will result in the wrong ads being sent to users and cause poor ad performance.
So, as publishers, how do we guarantee that we can deliver ads to audiences who will be interested in them and how can we decrease users' resistance to advertising? The answer is to collect information about the user requirements of your target audiences or to really know them, and to do so in a way that causes the least annoyance. But when a user is unwilling to share their personal data, such as age, gender, and interests, with my app, placing an ad based on the page that the user is browsing is a good alternative.
For example, a user is reading an article in a news app about the fast-paced development of electric vehicles, rapidly improving battery technology, and the expansion of charging stations in cities. If the targeted advertising mechanism understands the context of the article, when users continue to read news articles in the app, they may see native ads from nearby car dealerships for test driving electric vehicles or ads about special offers for purchasing electric vehicles of a certain brand. In this way, user interests can be accurately discovered, and publishers can perform advertising based on the keywords and other metadata included in the contextual information of the app page, or any other content users are reading or watching, without having to collect users' personal information.
But I can't integrate these features all by myself, so I started searching for tools to help me request and deliver ads based on the contextual information on an app page. That's when I had the great fortune to discover Ads Kit of HMS Core. Ads Kit supports personalized and non-personalized ads. Personalized ad requests require users to grant the app access to some of their personal information, which may not be palatable for some users. Non-personalized advertising, however, is not constrained by this requirement.
Non-personalized ads are not based on users' past behavior. Instead, they target audiences using contextual information. The contextual information includes the user's rough geographical location (such as city) authorized by the user, basic device information (such as the mobile phone model), and content of the current app or search keyword. When a user browses a piece of content in your app, or searches for a topic or keyword to express a specific interest, the contextual ad system scans a specific word or a combination of words, and pushes an ad based on the page content that the user is browsing.
Today, data security and personal privacy requirements are becoming more and more stringent. Many users are very hesitant to provide personal information, which means that precise ad delivery is becoming harder and harder to achieve. Luckily, Ads Kit requests ads based on contextual information, enabling publishers to perform ad delivery with a high degree of accuracy while protecting user privacy and information.
Now let's take a look at the simple steps we need to perform in order to quickly integrate Ads Kit and perform contextual advertising.
Integration Steps​
1. Ensure that the following prerequisites are met before you integrate the Ads Kit:
HMS Core (APK) 4.0.0.300 or later should be installed on devices. If the APK is not installed or an earlier version has been installed, you will not be able to call the APIs of the Ads Kit.
Before you begin the integration process, make sure that you have registered as a Huawei developer and completed identity verification on HUAWEI Developers.
Create a project and add an app to the project for later SDK integration.
2. Import the Ads SDK.
You can integrate the Ads SDK using the Maven repository.
That is, before you start developing an app, configure the Maven repository address for Ads SDK integration in your Android Studio project.
The procedure for configuring the Maven repository address in Android Studio is different for Gradle plugin versions earlier than 7.0, Gradle plugin 7.0, and Gradle plugin versions 7.1 and later. Configure the Maven repository address accordingly based on your Gadle plugin version.
3. Configure network permissions.
To allow apps to use cleartext HTTP and HTTPS traffic on devices with targetSdkVersion 28 or later, configure the following information in the AndroidManifest.xml file:
Code:
<application
...
android:usesCleartextTraffic="true"
>
...
</application>
4. Configure obfuscation scripts.
Before building the APK, configure the obfuscation configuration file to prevent the SDK from being obfuscated.
Open the obfuscation configuration file proguard-rules.pro in your app's module directory of your Android project, and add configurations to exclude the SDK from obfuscation.
Code:
-keep class com.huawei.openalliance.ad.** { *; }
-keep class com.huawei.hms.ads.** { *; }
5. Initialize the SDK.
You can initialize the SDK in the activity, or initialize the SDK by calling the HwAds.init(Context context) API in the AdSampleApplication class upon app launch. The latter method is recommended, but you have to implement the AdSampleApplication class by yourself.
6. Request ads based on contextual information.
The SDK provides the setContentBundle method in the AdParam.Builder class for you to pass contextual information in an ad request.
The sample code is as follows:
Code:
RewardAd rewardAd = new RewardAd(this, rewardId);
AdParam.Builder adParam = new AdParam.Builder();
String mediaContent = "{\"channelCategoryCode\":[\"TV series\"],\"title\":[\"Game of Thrones\"],\"tags\":[\"fantasy\"],\"relatedPeople\":[\"David Benioff\"],\"content\":[\"Nine noble families fight for control over the lands of Westeros.\"],\"contentID\":[\"123123\"],\"category\":[\"classics\"],\"subcategory\":[\"fantasy drama\"],\"thirdCategory\":[\"mystery\"]}\n";
adParam.setContentBundle(mediaContent);
rewardAd.loadAd(adParam.build(), new RewardAdLoadListener());
Conclusion​Nowadays, advertising is an important way for publishers to monetize their apps and content, and how to deliver the right ads to the right audiences has become a key focus point. In addition to creating high quality ads, significant efforts should be placed on ensuring precise ad delivery. As an app developer and publisher, I was always searching for methods to improve ad performance and content monetization in my app. In this article, I briefly introduced a useful tool, Ads Kit, which helps publishers request ads based on contextual information, without needing to collect users' personal information. What's more, the integration process is quick and easy and only involves a few simple steps. I'm sure you'll find it useful for improving your app's ad performance.
References​Development Guide of Ads Kit

Categories

Resources