[Huawei Developers] muraliameakula:
Overview

In this article, I will create a demo app along with the integration of OneSignal which is based on Cross platform Technology Xamarin. It provides messages that are "pushed" from a server and pop up on a user's device, even if the app is not in running state. They are a powerful re-engagement tool meant to provide actionable and timely information to subscribers.

OneSignal Service Introduction

OneSignal is the fastest and most reliable service to send push notifications, in-app messages, and emails to your users on mobile and web, including content management platforms like WordPress and Shopify. Users can discover resources and training to implement One Signal’s SDKs.

Prerequisite

1. Xamarin Framework

2. Huawei phone

3. Visual Studio 2019

4. OneSignal Account

App Gallery Integration process

1. Sign In and Create or Choose a project on AppGallery Connect portal.

2. Navigate to Project settings and download the configuration file.

3. Navigate to General Information, and then provide Data Storage location.

OneSignal SDK Integration process

1. Choose Huawei Android (HMS) and provide app name.

2. Choose Xamarin then click Next.

3. Copy your App Id.

4. Create New Push message from One Signal’s Dashboard.

5. Find Review Your Message tab, then click Send Message button.

Installing the Huawei ML NuGet package

1. Navigate to Solution Explore > Project > Right Click > Manage NuGet Packages.

2. Search on Browser Com.OneSignal and Install the package.

Xamarin App Development

1. Open Visual Studio 2019 and Create A New Project.

2. Configure Manifest file and add following permissions and tags.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="com.hms.onesignal">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" ></uses-sdk>
<permission android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">

<receiver android:name="com.onesignal.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>

</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

3. Create Activity class with XML UI.

MainActivity.cs

This activity performs all the operation regarding Push notification.

using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;

namespace OneSignalDemo
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);

Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);

}

private void setUpOneSignal()
{
OneSignal.Current.SetLogLevel(LOG_LEVEL.VERBOSE, LOG_LEVEL.NONE);

OneSignal.Current.StartInit("83814abc-7aad-454a-9d20-34e3681efcd1")
.InFocusDisplaying(OSInFocusDisplayOption.Notification)
.EndInit();
}

public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu_main, menu);
return true;
}

public override bool OnOptionsItemSelected(IMenuItem item)
{
int id = item.ItemId;
if (id == Resource.Id.action_settings)
{
return true;
}

return base.OnOptionsItemSelected(item);
}

private void FabOnClick(object sender, EventArgs eventArgs)
{
View view = (View) sender;
Snackbar.Make(view, "Replace with your own action", Snackbar.LengthLong)
.SetAction("Action", (Android.Views.View.IOnClickListener)null).Show();
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}

Xamarin App Build Result

1. Navigate to Build > Build Solution.

2. Navigate to Solution Explore > Project > Right Click > Archive/View Archive to generate SHA-256 for build release and Click on Distribute.

3. Choose Archive > Distribute.

4. Choose Distribution Channel > Ad Hoc to sign apk.

5. Choose Demo keystore to release apk.

6. Build succeed and click Save.

7. Result.

Message Deliver statistics

Tips and Tricks

Notification Types-25 means OneSignal timed out waiting for a response from Huawei's HMS to get a push token. This is most likely due to another 3rd-party HMS push SDK or your own HmsMessageService getting this event instead of OneSignal.

Conclusion

In this article, we have learned how to integrate OneSignal Push Notification in Xamarin based Android application. Developer can send OneSignal’s Push Message to users for new updates or any other information.

Thanks for reading this article. Be sure to like and comment to this article, if you found it helpful. It means a lot to me.

References

OneSignal Docs: click here

OneSignal Developer: click here

Original Source