Note: This integration is needed in addition to the integration steps outlined in the Xamarin integration page found here.
Integration
There are three files needed to add the Xtremepush functionality to your Xamarin.Forms app. These are the Xtremepush interface, and its implementation for the Android and iOS platforms.
Add the IXPushDelegate to the shared code section of your app.
Now open the IXPushDelegate and be sure to change the namespace to your own.
Now add the XPushDelegate.cs files from the Android and iOS folders to the relevant platform section. Again, afterwards, you will need to open these files and change the namespace to your own.
For Android, you will also need to add the nuget package Plugin.CurrentActivity.
Using Xtremepush
Once the project has been set up with a common interface and implementations for each platform, use DependencyService to get the right implementation at runtime.
Example to call hitTag
DependencyService.Get<IXPushDelegate>().HitTag("SampleTag");
The APIs available are
Using GetPushList
Andoid:
On android, to handle the returned Message list, you must implement the IPushListListener interface.
This interface has a single method 'PushListReceived'. An example is shown below.
public class MainApplication : Application, Application.IActivityLifecycleCallbacks, IPushListListener { ... public void PushListReceived(IList p0, Java.Lang.Ref.WeakReference p1) { foreach (Message m in p0){ } } }
iOS
On iOS, in the XPushDelagate.cs file, GetPushList takes a callback method as its third Parameter. This is set as default, to a method called PushListCallback, which is also in the file. However you can change this to another method if you wish.
An example of an implementation on iOS would be the following:
public void GetPushList(uint offset, uint limit) { //replace null with callback function XP.XPush.GetPushNotificationsOffset(offset, limit, PushListCallback); } public void PushListCallback(Foundation.NSArray arg1, Foundation.NSError arg2) { for (nuint i = 0; i < arg1.Count; i++){
XP.XPMessage t = arg1.GetItem(i); } }
Xtremepush Message Object
The XPush Message object contains the following keys:
ID: The ID of the message
Title : The title from the message
Text : The text of the message
Campaign ID : The id of the Xtremepush campaign
Data: Data includes additional information such as payloads associated with message
MetaData: Additional data about the message, such as create_time and read (user clicked message)
MetaData Example
Android
public void PushListReceived(IList p0, Java.Lang.Ref.WeakReference p1) { foreach (Message m in p0){ Log.Debug("XPushExample", "metadata"+m.Metadata["create_time"]); } }
iOS
public void PushListCallback(Foundation.NSArray arg1, Foundation.NSError arg2) { XP.XPMessage t = arg1.GetItem(0); for (nuint i = 0; i < arg1.Count; i++) { XP.XPMessage xpm = arg1.GetItem(i); System.Diagnostics.Debug.WriteLine("Metadata = {" +"create_time = "+xpm.Metadata["create_time"]+", read = "+xpm.Metadata["read"]); } }
Comments
0 comments
Article is closed for comments.