Implementation of push notification using oracle maf - oracle-maf

Iam able to register using device token.but I didn't receive message from my server.I have registered my project with GCM and got project Id ,server key.
enter code here
public void onMessage(Event event) {
String msg;
msg = event.getPayload();
System.out.println("#### Message from the Server :" + msg);
String one="";
AdfmfContainerUtilities.invokeContainerJavaScriptFunction(AdfmfJavaUtilities.getFeatureId(), "usercheck", new Object[]{msg});
// Parse the payload of the push notification
HashMap payload = null;
String pushMsg = "No message received";
try
{
payload = (HashMap)JSONBeanSerializationHelper.fromJSON(HashMap.class, msg);
pushMsg = (String)payload.get("alert");
}
catch(Exception e) {
e.printStackTrace();
}
// Write the push message to app scope to display to the user
AdfmfJavaUtilities.setELValue("#{applicationScope.pushMessage}", pushMsg);
}
public void onError(AdfException adfException) {
System.out.println("#### Error: " + adfException.toString());
// Write the error into app scope
AdfmfJavaUtilities.setELValue("#{applicationScope.errorMessage}", adfException.toString());
}
public void onOpen(String token) {
System.out.println("#### Registration token:" + token);
// Clear error in app scope
AdfmfJavaUtilities.setELValue("#{applicationScope.errorMessage}", null);
// Write the token into app scope
AdfmfJavaUtilities.setELValue("#{applicationScope.deviceToken}", token);
}
}

I think this article will help you. I was able to get them done for GCM but not for APN yet.
For registering with Google Cloud Messaging, go to this link, then click on "Get configuration" button.
In the server application, set your device token and sender id properly. The message is sent using these.

Since you got the deviceId from the server then i think the issue is not with the code. Did you enable PushPlugin in maf-application.xml ?

Related

Xamarin IOS - Show local notification when application is closed

I have a Xamarin IOS application that get's the users location each 10 sec, even when the app is killed. I make us of this library: "https://jamesmontemagno.github.io/GeolocatorPlugin/".
What I want is: When the app is closed or open and the user is at a specific location, I want to show a local notification. Is that even possible when the app is closed? I can't find information on this because it's always about remote notifications.
Notification permission should be requested as soon as the app launches by adding the following code to the FinishedLaunching method of the AppDelegate and setting the desired notification type (UNAuthorizationOptions):
...
using UserNotifications;
...
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
....
//after iOS 10
if(UIDevice.CurrentDevice.CheckSystemVersion(10,0))
{
UNUserNotificationCenter center = UNUserNotificationCenter.Current;
center.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.UNAuthorizationOptions.Badge, (bool arg1, NSError arg2) =>
{
});
center.Delegate = new NotificationDelegate();
}
else if(UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert| UIUserNotificationType.Badge| UIUserNotificationType.Sound,new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
return true;
}
New to iOS 10, an app can handle Notifications differently when it is in the foreground and a Notification is triggered. By providing a UNUserNotificationCenterDelegate and implementing the UserNotificationCentermethod, the app can take over responsibility for displaying the Notification. For example:
using System;
using ObjCRuntime;
using UserNotifications;
namespace workplat
{
public class NotificationDelegate:UNUserNotificationCenterDelegate
{
public NotificationDelegate()
{
}
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
// Do something with the notification
Console.WriteLine("Active Notification: {0}", notification);
// Tell system to display the notification anyway or use
// `None` to say we have handled the display locally.
completionHandler(UNNotificationPresentationOptions.Alert|UNNotificationPresentationOptions.Sound);
}
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
// Take action based on Action ID
switch (response.ActionIdentifier)
{
case "reply":
// Do something
break;
default:
// Take action based on identifier
if (response.IsDefaultAction)
{
// Handle default action...
}
else if (response.IsDismissAction)
{
// Handle dismiss action
}
break;
}
// Inform caller it has been handled
completionHandler();
}
}
}
To create and register a Custom Action with the system, use the following code:
public void RegisterNotification(long time)
{
UNUserNotificationCenter center = UNUserNotificationCenter.Current;
//creat a UNMutableNotificationContent which contains your notification content
UNMutableNotificationContent notificationContent = new UNMutableNotificationContent();
notificationContent.Title = "xxx";
notificationContent.Body= "xxxx";
notificationContent.Sound = UNNotificationSound.Default;
UNTimeIntervalNotificationTrigger trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(time, false);
UNNotificationRequest request = UNNotificationRequest.FromIdentifier("FiveSecond", notificationContent, trigger);
center.AddNotificationRequest(request,(NSError obj) =>
{
});
}
When you call this method ,for emample:
RegisterNotification(20);//set the time you want to push notification
The notification will been pushed after 20 seconds,enen if you close your app. You could put this line after uploading the location .
I have upload my demo to my github, you can download it for your reference: Demo Link .
And you can access the link for more information and details: MicroSoft Document

Xamarin Forms IOS with Azure Notification Hub

I know this question is asked multiple times. So bear with me here.
I am trying to implement Azure Notification Hub with My Android / iOS clients using Xamarin Forms.
For Android its going ok but for iOS I am not receiving any Notifications.
Here is a checklist of what I have done for iOS
Generated the Certificates and Profiles
Registered the Certificate with a MacOS
Exported the p12 file and Imported in Azure Hub
I also added the permissions in Info.plist (background remote notification)
No Changes in the Entitlements.plist file
I am able to get the Device token from the APN Service
I am able to register onto the Azure Hub after getting the Token
But I am unable to receive any notification and also none of the functions are called(DidRegisterUserNotificationSettings,WillPresentNotification,ReceivedRemoteNotification,DidRegisterUserNotificationSettings,FailedToRegisterForRemoteNotifications,DidReceiveRemoteNotification)
But RegisteredForRemoteNotifications is called
I also tested the devicetoken with or without removing the '<> ' from it but no success.
What may be the issue?
Here is my code
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
GlobalVariables.Instance.PnsToken = deviceToken.Description
.Replace("<", string.Empty)
.Replace(">", string.Empty)
.Replace(" ", string.Empty)
.ToUpper();
RegisteredForHub(application, deviceToken);
}
public void RegisteredForHub(UIApplication application, NSData deviceToken)
{
string pnsHandle = deviceToken.Description
.Replace("<", string.Empty)
.Replace(">", string.Empty)
.Replace(" ", string.Empty)
.ToUpper();
_hub = new SBNotificationHub(Constant.ListenConnectionString, Constant.NotificationHubName);
_hub.UnregisterNative((error) =>
{
if (error != null)
{
Debug.WriteLine($"Unable to call unregister Native {error}");
}
});
_hub.UnregisterTemplate("defaultTemplate",(error) =>
{
if (error != null)
{
Debug.WriteLine($"Unable to call unregister Template {error}");
}
});
// update registration with Azure Notification Hub
_hub.UnregisterAll(deviceToken, (error) =>
{
if (error != null)
{
Debug.WriteLine($"Unable to call unregister {error}");
return;
}
var tags = new NSSet(Constant.SubscribeTags);
_hub.RegisterNative(pnsHandle, tags, (errorCallback) =>
{
if (errorCallback != null)
{
Debug.WriteLine($"RegisterNativeAsync error: {errorCallback}");
}
});
var templateExpiration = DateTime.Now.AddDays(120)
.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
//_hub.RegisterTemplate(deviceToken, "defaultTemplate", Constant.ApnTemplateBody, templateExpiration,
// tags, (errorCallback) =>
// {
// if (errorCallback != null)
// {
// Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
// }
// });
});
After alot of trials and errors and so many hours googling. I found out the issue was with the Azure.
I had uploaded the the correct certificate but the password was one character of due to which no notifications were sent.
Azure should prompt the user for APNs if the user has entered the wrong password for its Certificate file.

Azure Notification HUB with FCM - NullPointerException

I am trying to register my FCM service with Azure Notification hub.
I am getting a valid FCM token using my Instance Id Service which extends FirebaseMessagingService().
Below is my InstanceIdService which is returning a valid token and starts my registrationIntentService
class FCMInstanceIdService : FirebaseMessagingService() {
companion object {
private const val TAG = "FCMInstanceIDService"
}
init {
Log.i(TAG, "init")
}
override fun onNewToken(refreshedToken: String?) {
FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener { instanceIdResult ->
instanceIdResult.token
// Log the event
Log.i(TAG, "Refreshing GCM Registration Token")
// Declare our intent to start the service
val intent = Intent(this, FCMRegistrationIntentService::class.java)
// Start the service!
startService(intent)
}
}
}
Below is the portion of code where-in I try to register this token with my notification hub.
// Declare the azure notification hub
val notificationHub = NotificationHub(
// Provide our notification hub name
CentreMK.FCM_NOTIFICATION_HUB_NAME,
// Provide our notification hub connection string
CentreMK.FCM_NOTIFICATION_HUB_LISTEN_CONNECTION_STRING,
// The context of this service
this)
// Log the event
Log.i(TAG, "Attempting to register with NH using token : $token")
// Update the registration id by registering our token with the notification hub
// This provides us with the registration id
regID = notificationHub.register(token).registrationId
This is the exception I get :
Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
Any help is appreciated cause I dont know how I could possibly get a null pointer exception when I've configured everything correctly.
In your initialisation of the String HubListenConnectionStringin class:
public class NotificationSettings {
public static String SenderId = "<Your project number>";
public static String HubName = "<Your HubName>";
public static String HubListenConnectionString = "<Enter your DefaultListenSharedAccessSignature connection string>";
}
you did not add "Endpoint=sb://"to the beginning of the string (i.e: public static String HubListenConnectionString ="Endpoint=sb://......". After which you might encounter a "Resource not found" exception error. That's where I am stuck at now.

Error while processing request in AzureMobile Apps HTTP2 error

This question is specific to a lately strange behavior of the Azure mobile Apps Android sdk. Everything was working fine for weeks. Now, my android client app suddenly can't connect to my web app any more. A Toast says "Error while processing request". In Android Studio debugger, I found the exception inside the SDK file MobileServiceConnection.java.
java.io.IOException: stream was reset: PROTOCOL_ERROR
In Azure Portal, my app shows "Healthy" status, but I can see the HTTP errors. Please help.
Following is my code, which was working fine and now throws error.
// Create the Mobile Service Client instance, using the provided mobile app URL.
try {
mClient = new MobileServiceClient(mMobileBackendUrl, activityContext).withFilter(
new ServiceFilter() {
#Override
public ListenableFuture<ServiceFilterResponse> handleRequest(ServiceFilterRequest request, NextServiceFilterCallback nextServiceFilter) {
// Get the request contents
String url = request.getUrl();
String content = request.getContent();
if (url != null) {
Log.d("Request URL:", url);
}
if (content != null) {
Log.d("Request Content:", content);
}
// Execute the next service filter in the chain
ListenableFuture<ServiceFilterResponse> responseFuture = nextServiceFilter.onNext(request);
Futures.addCallback(responseFuture, new FutureCallback<ServiceFilterResponse>() {
#Override
public void onFailure(Throwable exception) {
Log.d("Exception:", exception.getMessage());
}
#Override
public void onSuccess(ServiceFilterResponse response) {
if (response != null && response.getContent() != null) {
Log.d("Response Content:", response.getContent());
}
}
});
return responseFuture;
}
}
);
setAzureClient(mClient);
}catch(MalformedURLException e){
createAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
}catch(Exception e){
createAndShowDialog("There was an error creating the Mobile Service. "+ e.toString(), "Error");
}
Toast.makeText(context, context.getString(R.string.online_authentication), Toast.LENGTH_SHORT).show();
authenticate();
}
private void authenticate() { // give access only to authenticated users via Google account authentication
HashMap<String, String> parameters = new HashMap<>();
parameters.put("access_type", "offline");//use "Refresh tokens"
//login with the Google provider. This will create a call to onActivityResult() method inside the context Activity, which will then call the onActivityResult() below.
mClient.login(MobileServiceAuthenticationProvider.Google, url_scheme_of_your_app, GOOGLE_LOGIN_REQUEST_CODE, parameters);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// When request completes
if (requestCode == 1) {
try {
MobileServiceActivityResult result = mClient.onActivityResult(data);
if (result.isLoggedIn()) {
Toast.makeText(context, context.getString(R.string.azure_auth_login_success) /*+ " " + mClient.getCurrentUser().getUserId()*/, Toast.LENGTH_SHORT).show();
mUserId = mClient.getCurrentUser().getUserId();
} else {//>>>>THIS IS WHERE I AM GETTING THE ERROR
String errorMessage = result.getErrorMessage();
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show();// Error While processing request (it comes form the MobileServiceConnection.java file inside sdk)
}
}catch(Exception e){
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
I found the answer myself. The error was due to an Azure App Service HTTP2 connection issue. It has nothing to do with the app code. For anyone facing the same problem, here is the solution.
Go to https://resources.azure.com/
Make sure you are in Read/Write mode by clicking in the option to the left of your name.
From the left column, browse to: https://resources.azure.com/subscriptions/yourSubscriptionId/resourceGroups/yourWebAppResourceGroup/providers/Microsoft.Web/sites/yourWebAppName/config/web
Find and Change the property: "http20Enabled": from true to false by clicking EDIT, Update value to “false” and then clicking in Save or PATCH.

deviceToken of IOS Device

I am using Monotouch for mac and have gone through the steps to retrieve a provisioning profile certificate enabling push notification in the process. I have a working app and am now experimenting with apns-sharp and moon-apns but cant' figure out how to retrieve my device token. I'm hoping someone can provide me with detailed and straightforward steps to achieve this.
In your FinishedLaunching method, register the app for remote notifications, through the UIApplication object you get in it:
// Pass the UIRemoteNotificationType combinations you want
app.RegisterForRemoteNotificationTypes(UIRemoteNotificationType.Alert |
UIRemoteNotificationType.Sound);
Then, in your AppDelegate class, override the RegisteredForRemoteNotifications method:
public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
{
// The device token
byte[] token = deviceToken.ToArray();
}
You also have to override the FailedToRegisterForRemoteNotifications method, to handle the error, if any:
public override void FailedToRegisterForRemoteNotifications (UIApplication application, NSError error)
{
// Do something with the error
}
As of iOS deviceToken has changed. The following code worked for me to convert deviceToken as NSData to a string.
string deviceTokenString;
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
deviceTokenString = BitConverter.ToString(deviceToken.ToArray()).Replace("-", string.Empty);
}
else
{
deviceTokenString = Regex.Replace(deviceToken.ToString(), "[^0-9a-zA-Z]+", string.Empty);
}
For me this was only half of the resolution. To use the DeviceToken from a webserver (PHP in my case), the DeviceToken needs to be a Hex String used in the PHP code for firing the Push Notification (e.g. as shown here: [Using PHP to send iOS Push Notifications via APNs
However, the NSdata object offers no simple way to provide that Hex String.
So my "RegisteredForRemoteNotifications" success handler is now:
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
// Get current device token
var DeviceToken = Tools.ByteToHex(deviceToken.ToArray());
string DeviceID = UIDevice.CurrentDevice.IdentifierForVendor.AsString();
System.Console.WriteLine("### UserNotification Device Token = " + DeviceToken + ", DeviceID = " + DeviceID);
// Get previous device token
var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");
// Has the token changed?
if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
{
//### todo: Populate POSTdata set
//### todo: Send POSTdata to URL
// Save new device token
NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
}
}
And for the Byte to Hex Conversion:
public static string ByteToHex(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 2);
foreach (byte b in data)
{
sb.AppendFormat("{0:x2}", b);
}
return sb.ToString();
}
Now you can use the DeviceToken in PHP to create the PushNotification submission.

Resources