Push sharp not sending notification and No Call backs in Push service - push-notification

I am using pushsharp to create remote notification to my device. I did all the certficate stuff. My problem is when I try to send notification using push sharp nothing happens here is my code I adopted from Here.
I would atleast expect some of the callbacks to be called like NotificationFailed, ChanelCreated...etc
Here is my code
public class PushNotificationService
{
private static PushNotificationApple _pushNotificationApple;
public static void Main()
{
PushNotificationService ser = new PushNotificationService();
string token = "0eaaf46d379bc5f5529f0b3357e0973ccd4655b163d789d875e3ad5fe64210d9";
ser.SendPushNotification(token, "Hello my push");
System.Console.WriteLine("Test");
}
public PushNotificationService()
{
if (_pushNotificationApple == null)
{
_pushNotificationApple = new PushNotificationApple();
}
}
/// <summary>
/// Send the push notification to the device
/// </summary>
/// <param name="deviceToken">The device token</param>
/// <param name="message">The message</param>
/// <returns>True if the notification is sent</returns>
public bool SendPushNotification(string deviceToken, string message)
{
if (_pushNotificationApple != null)
{
_pushNotificationApple.SendNotification(deviceToken, message);
}
return true;
}
}
Another class
/// <summary>
/// Class for
/// </summary>
public class PushNotificationApple
{
private static PushBroker pushBroker;
/// <summary>
/// The push notification apple
/// </summary>
public PushNotificationApple()
{
if (pushBroker == null)
{
//Create our push services broker
pushBroker = new PushBroker();
//Wire up the events for all the services that the broker registers
pushBroker.OnNotificationSent += NotificationSent;
pushBroker.OnChannelException += ChannelException;
pushBroker.OnServiceException += ServiceException;
pushBroker.OnNotificationFailed += NotificationFailed;
pushBroker.OnNotificationRequeue += NotificationRequeue;
pushBroker.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
pushBroker.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
pushBroker.OnChannelCreated += ChannelCreated;
pushBroker.OnChannelDestroyed += ChannelDestroyed;
//-------------------------
// APPLE NOTIFICATIONS
//-------------------------
//Configure and start Apple APNS
// IMPORTANT: Make sure you use the right Push certificate. Apple allows you to generate one for connecting to Sandbox,
// and one for connecting to Production. You must use the right one, to match the provisioning profile you build your
// app with!
// Make sure you provide the correct path to the certificate, in my case this is how I did it in a WCF service under Azure,
// but in your case this might be different. Putting the .p12 certificate in the main directory of your service
// (in case you have a webservice) is never a good idea, people can download it from there..
//System.Web.Hosting.HostingEnvironment.MapPath("~/folder/file");
var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Resources/PushNotification.p12"));
//var appleCert = File.ReadAllBytes("/Resources/PushNotification.p12");
// TODD revise
// var appleCert = File.ReadAllBytes(System.Web.Hosting.HostingEnvironment.MapPath("~/PushSharp.PushCert.Development.p12"));
//IMPORTANT: If you are using a Development provisioning Profile, you must use the Sandbox push notification server
// (so you would leave the first arg in the ctor of ApplePushChannelSettings as 'false')
// If you are using an AdHoc or AppStore provisioning profile, you must use the Production push notification server
// (so you would change the first arg in the ctor of ApplePushChannelSettings to 'true')
pushBroker.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, "")); //Extension method
}
}
private void NotificationRequeue(object sender, PushSharp.Core.NotificationRequeueEventArgs e)
{
Console.WriteLine("Chanel Notification Requeue");
}
public void SendNotification(string deviceToken, string message)
{
//Fluent construction of an iOS notification
//IMPORTANT: For iOS you MUST MUST MUST use your own DeviceToken here that gets generated within your iOS app itself when the Application Delegate
// for registered for remote notifications is called, and the device token is passed back to you
if (pushBroker != null)
{
pushBroker.QueueNotification(new AppleNotification()
.ForDeviceToken(deviceToken)
.WithAlert(message)
.WithBadge(10)
.WithSound("sound.caf"));
}
}
private void ChannelDestroyed(object sender)
{
Console.WriteLine("Chanel Destroyed");
}
private void ChannelCreated(object sender, PushSharp.Core.IPushChannel pushChannel)
{
Console.WriteLine("Chanel created");
}
private void DeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, PushSharp.Core.INotification notification)
{
Console.WriteLine("Device Subscription Changed");
}
private void DeviceSubscriptionExpired(object sender, string expiredSubscriptionId, DateTime expirationDateUtc, PushSharp.Core.INotification notification)
{
Console.WriteLine("Device Subscription Expired");
}
private void NotificationFailed(object sender, PushSharp.Core.INotification notification, Exception error)
{
Console.WriteLine("Notification Failed");
}
private void ServiceException(object sender, Exception error)
{
Console.WriteLine("Service Exception");
}
private void ChannelException(object sender, PushSharp.Core.IPushChannel pushChannel, Exception error)
{
Console.WriteLine("Channel Exception");
}
private void NotificationSent(object sender, PushSharp.Core.INotification notification)
{
Console.WriteLine("Notification Sent");
}
}

This solved my problem:
While creating Production SSL certificate, do not change the name "aps_production.cer".
And before creating Development related certificates, first create certificates(SSL,provisioning,p12) for only production and try.
It really worked for me after trying different approaches. Give it a try.

Adding this line at the end of the method SendNotification() solved my problem.
pushBroker.StopAllServices();

Related

Using firebase cloud messaging for user to user push notifications [duplicate]

I have been trying to read the official docs and guides about how to send message from one device to another. I have saved registration token of both devices in the Real Time Database, thus I have the registration token of another device.
I have tried the following way to send the message
RemoteMessage message = new RemoteMessage.Builder(getRegistrationToken())
.setMessageId(incrementIdAndGet())
.addData("message", "Hello")
.build();
FirebaseMessaging.getInstance().send(message);
However this is not working. The other device doesn't receive any message. I am not even sure, if I can use upstream message sending to conduct device to device communication.
PS: I just want to know if device-to-device messaging is possible using FCM? If yes, then is the code I used have some issue? If yes, then what is the correct way.
Update:
My question was to ask whether device to device messaging without using any separate server other than firebase could messaging is possible or not, if yes than how, since there's no documentation about it. I do not understand what is left to explain here? Anyways I got the answer and will update it as an answer once the question gets reopened.
Firebase has two features to send messages to devices:
the Notifications panel in your Firebase Console allows you to send notifications to specific devices, groups of users, or topics that users subscribed to.
by calling Firebase Cloud Messaging API, you can send messages with whatever targeting strategy you prefer. Calling the FCM API requires access to your Server key, which you should never expose on client devices. That's why you should always run such code on an app server.
The Firebase documentation shows this visually:
Sending messages from one device directly to another device is not supported through the Firebase Cloud Messaging client-side SDKs.
Update: I wrote a blog post detailing how to send notifications between Android devices using Firebase Database, Cloud Messaging and Node.js.
Update 2: You can now also use Cloud Functions for Firebase to send messages securely, without spinning up a server. See this sample use-case to get started. If you don't want to use Cloud Functions, you can run the same logic on any trusted environment you already have, such as your development machine, or a server you control.
Warning There is a very important reason why we don't mention this approach anywhere. This exposes your server key in the APK that
you put on every client device. It can (and thus will) be taken from
there and may lead to abuse of your project. I highly recommend
against taking this approach, except for apps that you only put on
your own devices. – Frank van Puffelen
Ok, so the answer by Frank was correct that Firebase does not natively support device to device messaging. However there's one loophole in that. The Firebase server doesn't identify whether you have send the request from an actual server or are you doing it from your device.
So all you have to do is send a Post Request to Firebase's messaging server along with the Server Key. Just keep this in mind that the server key is not supposed to be on the device, but there's no other option if you want device-to-device messaging using Firebase Messaging.
I am using OkHTTP instead of default way of calling the Rest API. The code is something like this -
public static final String FCM_MESSAGE_URL = "https://fcm.googleapis.com/fcm/send";
OkHttpClient mClient = new OkHttpClient();
public void sendMessage(final JSONArray recipients, final String title, final String body, final String icon, final String message) {
new AsyncTask<String, String, String>() {
#Override
protected String doInBackground(String... params) {
try {
JSONObject root = new JSONObject();
JSONObject notification = new JSONObject();
notification.put("body", body);
notification.put("title", title);
notification.put("icon", icon);
JSONObject data = new JSONObject();
data.put("message", message);
root.put("notification", notification);
root.put("data", data);
root.put("registration_ids", recipients);
String result = postToFCM(root.toString());
Log.d(TAG, "Result: " + result);
return result;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
try {
JSONObject resultJson = new JSONObject(result);
int success, failure;
success = resultJson.getInt("success");
failure = resultJson.getInt("failure");
Toast.makeText(getCurrentActivity(), "Message Success: " + success + "Message Failed: " + failure, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getCurrentActivity(), "Message Failed, Unknown error occurred.", Toast.LENGTH_LONG).show();
}
}
}.execute();
}
String postToFCM(String bodyString) throws IOException {
RequestBody body = RequestBody.create(JSON, bodyString);
Request request = new Request.Builder()
.url(FCM_MESSAGE_URL)
.post(body)
.addHeader("Authorization", "key=" + SERVER_KEY)
.build();
Response response = mClient.newCall(request).execute();
return response.body().string();
}
I hope Firebase will come with a better solution in future. But till then, I think this is the only way. The other way would be to send topic message or group messaging. But that was not in the scope of the question.
Update:
The JSONArray is defined like this -
JSONArray regArray = new JSONArray(regIds);
regIds is a String array of registration ids, you want to send this message to. Keep in mind that the registration ids must always be in an array, even if you want it to send to a single recipient.
I have also been using direct device to device gcm messaging in my prototype. It has been working very well. We dont have any server. We exchange GCM reg id using sms/text and then communicate using GCM after that. I am putting here code related to GCM handling
**************Sending GCM Message*************
//Sends gcm message Asynchronously
public class GCM_Sender extends IntentService{
final String API_KEY = "****************************************";
//Empty constructor
public GCM_Sender() {
super("GCM_Sender");
}
//Processes gcm send messages
#Override
protected void onHandleIntent(Intent intent) {
Log.d("Action Service", "GCM_Sender Service Started");
//Get message from intent
String msg = intent.getStringExtra("msg");
msg = "\"" + msg + "\"";
try{
String ControllerRegistrationId = null;
//Check registration id in db
if(RegistrationIdAdapter.getInstance(getApplicationContext()).getRegIds().size() > 0 ) {
String controllerRegIdArray[] = RegistrationIdAdapter.getInstance(getApplicationContext()).getRegIds().get(1);
if(controllerRegIdArray.length>0)
ControllerRegistrationId = controllerRegIdArray[controllerRegIdArray.length-1];
if(!ControllerRegistrationId.equalsIgnoreCase("NULL")){
// 1. URL
URL url = new URL("https://android.googleapis.com/gcm/send");
// 2. Open connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// 3. Specify POST method
urlConnection.setRequestMethod("POST");
// 4. Set the headers
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Authorization", "key=" + API_KEY);
urlConnection.setDoOutput(true);
// 5. Add JSON data into POST request body
JSONObject obj = new JSONObject("{\"time_to_live\": 0,\"delay_while_idle\": true,\"data\":{\"message\":" + msg + "},\"registration_ids\":[" + ControllerRegistrationId + "]}");
// 6. Get connection output stream
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
out.write(obj.toString());
out.close();
// 6. Get the response
int responseCode = urlConnection.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null){
response.append(inputLine);
}
in.close();
Log.d("GCM getResponseCode:", new Integer(responseCode).toString());
}else{
Log.d("GCM_Sender:","Field REGISTRATION_TABLE is null");
}
}else {
Log.d("GCM_Sender:","There is no Registration ID in DB ,please sync devices");
}
} catch (Exception e) {
e.printStackTrace();
//MessageSender.getInstance().sendMessage(msg, Commands.SMS_MESSAGE);
}
}
//Called when service is no longer alive
#Override
public void onDestroy() {
super.onDestroy();
//Do a log that GCM_Sender service has been destroyed
Log.d("Action Service", "GCM_Sender Service Destroyed");
}
}
**************Receiving GCM Message*************
public class GCM_Receiver extends WakefulBroadcastReceiver {
public static final String RETRY_ACTION ="com.google.android.c2dm.intent.RETRY";
public static final String REGISTRATION ="com.google.android.c2dm.intent.REGISTRATION";
public SharedPreferences preferences;
//Processes Gcm message .
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
//Start GCMNotificationIntentService to handle gcm message asynchronously
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
/*//Check if DatabaseService is running .
if(!DatabaseService.isServiceRunning) {
Intent dbService = new Intent(context,DatabaseService.class);
context.startService(dbService);
}*/
//Check if action is RETRY_ACTION ,if it is then do gcm registration again .
if(intent.getAction().equals(RETRY_ACTION)) {
String registrationId = intent.getStringExtra("registration_id");
if(TextUtils.isEmpty(registrationId)){
DeviceRegistrar.getInstance().register(context);
}else {
//Save registration id to prefs .
preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("BLACKBOX_REG_ID",registrationId);
editor.commit();
}
} else if (intent.getAction().equals(REGISTRATION)) {
}
}
}
//Processes gcm messages asynchronously .
public class GCMNotificationIntentService extends IntentService{
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
String gcmData;
private final String TAG = "GCMNotificationIntentService";
//Constructor with super().
public GCMNotificationIntentService() {
super("GcmIntentService");
}
//Called when startService() is called by its Client .
//Processes gcm messages .
#Override
protected void onHandleIntent(Intent intent) {
Log.d("GCMNotificationIntentService", "GCMNotificationIntentService Started");
Bundle extras = intent.getExtras();
//Get instance of GoogleCloudMessaging .
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
//Get gcm message type .
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
Log.i(TAG, "Completed work # " + SystemClock.elapsedRealtime());
gcmData = extras.getString("message");
Intent actionService = new Intent(getApplicationContext(),Action.class);
actionService.putExtra("data", gcmData);
//start Action service .
startService(actionService);
//Show push notification .
sendNotification("Action: " + gcmData);
//Process received gcmData.
Log.d(TAG,"Received Gcm Message from Controller : " + extras.getString("message"));
}
}
GCM_Receiver.completeWakefulIntent(intent);
}
//Shows notification on device notification bar .
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, BlackboxStarter.class);
//Clicking on GCM notification add new layer of app.
notificationIntent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("Notification from Controller")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
//Play default notification
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
//Called when service is no longer be available .
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("GCMNotificationIntentService", "GCMNotificationIntentService Destroyed");
}
}
According to the new documentation which was updated on October 2, 2018 you must send post request as below
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA //Server key
{
"to": "sent device's registration token",
"data": {
"hello": "message from someone",
}
}
To get device's registration token extend FirebaseMessagingService and override onNewToken(String token)
For more info refer to doc https://firebase.google.com/docs/cloud-messaging/android/device-group
I am late but above solutions has helped me to write down this simple answer, you can send your message directly to android devices from android application, here is the simple implementation I have done and it works great for me.
compile android volley library
compile 'com.android.volley:volley:1.0.0'
Just copy paste this simple function ;) and your life will become smooth just like knife in butter. :D
public static void sendPushToSingleInstance(final Context activity, final HashMap dataValue /*your data from the activity*/, final String instanceIdToken /*firebase instance token you will find in documentation that how to get this*/ ) {
final String url = "https://fcm.googleapis.com/fcm/send";
StringRequest myReq = new StringRequest(Request.Method.POST,url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(activity, "Bingo Success", Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(activity, "Oops error", Toast.LENGTH_SHORT).show();
}
}) {
#Override
public byte[] getBody() throws com.android.volley.AuthFailureError {
Map<String, Object> rawParameters = new Hashtable();
rawParameters.put("data", new JSONObject(dataValue));
rawParameters.put("to", instanceIdToken);
return new JSONObject(rawParameters).toString().getBytes();
};
public String getBodyContentType()
{
return "application/json; charset=utf-8";
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "key="+YOUR_LEGACY_SERVER_KEY_FROM_FIREBASE_CONSOLE);
headers.put("Content-Type","application/json");
return headers;
}
};
Volley.newRequestQueue(activity).add(myReq);
}
Note
If you want to send message to topics so you can change parameter instanceIdToken to something like /topics/topicName.
For groups implementation is the same but you just need to take care of parameters. checkout Firebase documentation and you can pass those parameters.
let me know if you face any issue.

Resume App from a Firebase Notification is not working (Xamarin Forms)

I am working on integrating firebase push notification to my application.
Please find my firebase FirebaseMessagingService class.
If the app is open and running everything is working fine. But if the app is not open / if I switch to some other app (my app is not closed). I am getting notification but when I tap on Notification it relaunches the app without resuming.
I am using launch mode LaunchMode = LaunchMode.SingleTop in my main activity.
And if the app is open I am getting the response in OnNewIntent override method of main activity.
Can anyone please help me to figure of the real cause. Please help.
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class DriverAppMessagingService : FirebaseMessagingService
{
#region Overriden Methods
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
var parameters = new Dictionary<string, object>();
var notification = message.GetNotification();
if (null != notification)
{
if (!string.IsNullOrEmpty(notification.Body))
{
parameters.Add("Body", notification.Body);
}
if (!string.IsNullOrEmpty(notification.BodyLocalizationKey))
{
parameters.Add("BodyLocalizationKey", notification.BodyLocalizationKey);
}
// convert the incoming message to a local notification
SendLocalNotification(parameters);
// send the incoming message directly to the MainActivty
SendNotificationToMainActivity(parameters);
}
}
public override void OnNewToken(string p0)
{
base.OnNewToken(p0);
//Persist the token to app settings for registration purpose.
AppDefinition.Helpers.Settings.Current.PnsHandle = p0;
}
#endregion
#region Private Methods
/// <summary>
///
/// </summary>
/// <param name="args"></param>
private void SendNotificationToMainActivity(Dictionary<string, object> args)
{
if (CrossCurrentActivity.Current.Activity is MainActivity activity)
{
var message = args["Body"].ToString();
activity.TriggerPushNotification(message);
}
}
/// <summary>
/// Method to trigger the local notification.
/// </summary>
/// <param name="args"></param>
private void SendLocalNotification(Dictionary<string, object> args)
{
//TODO Only using one token from message response.
var message = args["Body"].ToString();
var intent = new Intent(CrossCurrentActivity.Current.Activity, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("message", message);
var pendingIntent = PendingIntent.GetActivity(CrossCurrentActivity.Current.Activity, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(CrossCurrentActivity.Current.Activity, Constants.NotificationChannelName)
.SetContentTitle(Constants.ContentTitle)
.SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
.SetContentText(message)
.SetAutoCancel(true)
.SetShowWhen(false)
.SetLights(0xff0000, 100, 100)
.SetContentIntent(pendingIntent);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(Constants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(CrossCurrentActivity.Current.Activity);
notificationManager.Notify(0, notificationBuilder.Build());
}
#endregion
}
As you set the MainActivity LaunchMode = LaunchMode.SingleTop and set ActivityFlags.ClearTop,when you tap the notification to open your application,it will clear all activities above MainActivity and place MainActivity at the top of the stack.
Instead of recreating MainActivity, it enters the OnNewIntent method.
You could make a breakpoint in OnCreate method,when you open the application after tap the notification,it will not step into it.
Data messages - Handled by the client app. These messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed. When using this type of message you are the one providing the UI and handling when push notification is received on an Android device.
{
"data": {
"message" : "my_custom_value",
"other_key" : true,
"body":"test"
},
"priority": "high",
"condition": "'general' in topics"
}
Try this, this will solve your issue.

Pushsharp 4.0 with Firebase

After reading lot of post I could not find a complete example how to send GCM push notifications using Pushsharp 4.0 with Firebase. Lot of examples with PushSharp are using the old Google cloud messaging, not Firebase and/or the old PushSharp version.
Does anyone have an example of a stable and working code for sending GCM push notifications using PushSharp 4.0 with Firebase ?
You can try this: https://github.com/Redth/PushSharp/issues/711
I haven't tried for myself but from the comments in above post, it appears people had success with PushSharp and Firebase with the suggested change.
I was able to get PushSharp to work with FCM relatively easily. I thought it would be much harder. Here are the steps I took:
I created a new project on console.firebase.google.com
Then I went to settings => Cloud Messaging. I used that SenderID as my PushSharp sender ID and Legacy Server Key as my PushSharp Authentication Key.
Next, I went to the google play console, selected the project, then Development Tools => Services and API.
Then I linked the App project to the new FCM project by entering the SenderID in the "Linked Sender ID" field.
That should be all you do for the server to be working.
Now, I needed to hook up the App to this new SenderID. I think this is usually done by entering the google-services.json file as shown in the Firebase setup. However, I use PhoneGap/Cordova plug in for sending notifications. So, I do this:
Change the GCM_SENDER_ID to the SenderID above.
Unfortunately, I had to redistribute the new version of the app to get it to work. But it did work.
Hope this helps someone else.
This console program worked fine for me.
class Program
{
static void Main(string[] args)
{
try
{
string token = "putYourSecretTokenHere";
using (var s = new FcmPushNotificationService())
{
s.SendPushNotification(token);
Console.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadLine();
}
}
public sealed class FcmPushNotificationService : IDisposable
{
#region Constructors
public FcmPushNotificationService()
{
string serverKey = "Legacy server key";
this.Config = new GcmConfiguration(serverKey);
this.Config.GcmUrl = "https://fcm.googleapis.com/fcm/send";
this.Broker = this.InitializeBroker();
}
#endregion
#region Properties
private GcmServiceBroker Broker { get; }
private GcmConfiguration Config { get; }
#endregion
#region Private Methods
private GcmServiceBroker InitializeBroker()
{
var gcmServiceBroker = new GcmServiceBroker(this.Config);
gcmServiceBroker.OnNotificationSucceeded += this.OnNotificationSucceeded;
gcmServiceBroker.OnNotificationFailed += this.OnNotificationFailed;
gcmServiceBroker.Start();
return gcmServiceBroker;
}
#endregion
#region Event Handlers
private void OnNotificationFailed(GcmNotification gcmNotification, AggregateException aggregateEx)
{
aggregateEx.Handle(ex =>
{
// See what kind of exception it was to further diagnose
if (ex is GcmNotificationException notificationException)
{
Console.WriteLine($"Notification of {string.Join(", ", notificationException.Notification.RegistrationIds)} failed: {notificationException.Message}");
}
else if (ex is GcmMulticastResultException multicastException)
{
Console.WriteLine($"Notification of {string.Join(", ", multicastException.Succeeded.SelectMany(n => n.RegistrationIds))} succeeded.");
Console.WriteLine($"Notification of {string.Join(", ", multicastException.Failed.SelectMany(n => n.Key.RegistrationIds))} failed: {multicastException.Message}");
}
else if (ex is DeviceSubscriptionExpiredException expiredException)
{
Console.WriteLine($"Device registration id expired: {expiredException.OldSubscriptionId}. Device registration id changed to {expiredException.NewSubscriptionId}");
}
else if (ex is RetryAfterException retryException)
{
Console.WriteLine($"FCM rate limited, don't send more until after {retryException.RetryAfterUtc}");
}
else
{
Console.WriteLine($"Failed to send notification {ex}");
}
// Mark it as handled
return true;
});
}
private void OnNotificationSucceeded(GcmNotification gcmNotification)
{
Console.WriteLine($"Notification sent to {string.Join(", ", gcmNotification.RegistrationIds)}. Data: {gcmNotification.Data}, Notification: {gcmNotification.Notification}");
}
#endregion
#region IDisposable Members
/// <inheritdoc cref="IDisposable"/>
public void Dispose()
{
this.Broker?.Stop();
}
#endregion
#region IPushNotificationService Members
///<inheritdoc/>
public void SendPushNotification(string token)
{
var notification = JObject.Parse("{\"title\": \"Test\",\"body\": \"Success!\"}");
this.Broker.QueueNotification(new GcmNotification
{
RegistrationIds = new List<string> { token },
Notification = notification
});
}
#endregion
}
}

How to solve the issue like app service will not be call on every time?

I am working on Microsoft band technology, in one of my current scenario as I am sending Sms in background (means even app is not open in foreground also its working like sending Sms successfully).
For that I created the appservice by taking with Windows Runtime Component as a template.
In that app service I wrote the code for how to connect to the band and how to create the tile and how to register the Events.
And also wrote the code in Button_Pressed event for sending the Sms to others.
After that I configured the app service in package. Manifest file like this below figure.
enter image description here
My issue is appservice will not be call on every time.it will be call on two or three times per day.
Is the problem in my code or issue of Microsoft health app?
using Microsoft.Band;
using Microsoft.Band.Notifications;
using Microsoft.Band.Tiles;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
namespace AppServiceEvents
{
public sealed class MyTileEventHandler : IBackgroundTask
{
private BackgroundTaskDeferral backgroundTaskDeferral;
private AppServiceConnection appServiceConnection;
IBandClient bandClient;
//BandTile tile;
//IEnumerable<BandTile> tiles;
//Guid tileGuid;
public void Run(IBackgroundTaskInstance taskInstance)
{
//throw new NotImplementedException();
this.backgroundTaskDeferral = taskInstance.GetDeferral(); taskInstance.Canceled += OnTaskCanceled;
//await ConnectBand();
// Add handlers for tile events
BackgroundTileEventHandler.Instance.TileOpened += EventHandler_TileOpened;
BackgroundTileEventHandler.Instance.TileClosed += EventHandler_TileClosed;
BackgroundTileEventHandler.Instance.TileButtonPressed += EventHandler_TileButtonPressed;
// Set up handler for incoming app service request messages
var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;
this.appServiceConnection = details.AppServiceConnection;
this.appServiceConnection.RequestReceived +=OnRequestReceived;
}
private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
//throw new NotImplementedException();
var messageDeferral = args.GetDeferral();
ValueSet response = new ValueSet();
ValueSet request = args.Request.Message;
// Decode the received message and call the appropriate handler
BackgroundTileEventHandler.Instance.HandleTileEvent(request);
// Send the response
await args.Request.SendResponseAsync(response);
messageDeferral.Complete();
}
private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
//throw new NotImplementedException();
DisconnectBand();
if (this.backgroundTaskDeferral != null)
{
this.backgroundTaskDeferral.Complete();
}
}
private void EventHandler_TileOpened(object sender, BandTileEventArgs<IBandTileOpenedEvent> e)
{
// TODO: Handle the tile opening
LogEvent(String.Format("EventHandler_TileOpened: TileId={0} Timestamp={1}", e.TileEvent.TileId, e.TileEvent.Timestamp));
// We create a Band connection when the tile is opened and keep it connected until the tile closes.
ConnectBand();
}
private void EventHandler_TileClosed(object sender, BandTileEventArgs<IBandTileClosedEvent> e)
{
// TODO: Handle the tile closing
LogEvent(String.Format("EventHandler_TileClosed: TileId={0} Timestamp={1}", e.TileEvent.TileId, e.TileEvent.Timestamp));
// Disconnect the Band now that the user has closed the tile.
DisconnectBand();
}
private void EventHandler_TileButtonPressed(object sender, BandTileEventArgs<IBandTileButtonPressedEvent> e)
{
// TODO: Handle the button push
LogEvent(String.Format("EventHandler_TileButtonPressed: TileId={0} PageId={1} ElementId={2}", e.TileEvent.TileId, e.TileEvent.PageId, e.TileEvent.ElementId));
// We should have a Band connection from the tile open event, but in case the OS unloaded our background code
// between that event and this button press event, we restore the connection here as needed.
ConnectBand();
var tileid = e.TileEvent.TileId;
SendMessage(tileid);
//await new MessageDialog("This is an background task"+tileid).ShowAsync();
}
private void ConnectBand()
{
if (this.bandClient == null)
{
// Note that we specify isBackground = true here to avoid conflicting with any foreground app connection to the Band
Task<IBandInfo[]> getBands = BandClientManager.Instance.GetBandsAsync(isBackground: true);
getBands.Wait();
IBandInfo[] pairedBands = getBands.Result;
if (pairedBands.Length == 0)
{
LogEvent("ERROR - No paired Band");
}
try
{
Task<IBandClient> connect = BandClientManager.Instance.ConnectAsync(pairedBands[0]);
connect.Wait();
this.bandClient = connect.Result;
}
catch
{
LogEvent("ERROR - Unable to connect to Band");
}
}
}
/// <summary>
/// If currently connected to the Band, then disconnect.
/// </summary>
private void DisconnectBand()
{
if (bandClient != null)
{
bandClient.Dispose();
bandClient = null;
}
}
const string LogFileName = "EventLog.txt";
/// <summary>
/// Log event strings to a text file
/// </summary>
/// <param name="eventString">String describing the event</param>
private void LogEvent(string eventString)
{
using (FileStream stream = new FileStream("EventLog.txt", FileMode.Append))
{
string outputString = String.Format("{0}: {1}\r\n", DateTime.Now, eventString);
byte[] outputASCII = Encoding.ASCII.GetBytes(outputString);
stream.Write(outputASCII, 0, outputASCII.Length);
}
}
private async void SendMessage(Guid tileGuid)
{
try
{ // Send a message to the Band for one of our tiles,
// and show it as a dialog.
await bandClient.NotificationManager.SendMessageAsync(tileGuid, "Task", "This is an AppService Task", DateTimeOffset.Now, MessageFlags.ShowDialog);
}
catch (BandException ex)
{
// handle a Band connection exception
}
}
}
}
Please tell me how to resolve the above issue and tell me how to debug the appservice in VS 2015 but not a background task.
Regards,
Pradeep

EWS Notification Hub for multiple users

I am trying to create a service to push exchange notifications to asp.net applications, eventually using SignalR.
My plan is to create a notification hub that subscribes each user as they log in to the asp application and listen for notifications for them. As a notification is received the second part of the project is to then use signalR to only send the correct notifications to each user. Once they log out or time out the notification hub will unsubscribe them.
So far I have done a little basic testing and can receive notifications in a little console app for myself with my credentials hard coded. What I am struggling with is how to do this for multiple people simultaneously. For example would I have to create separate threads of this for each user or is there a better way?
I guess regardless I am going to have to use impersonation rather than holding each users credentials right? I'm also going to have to work out a way to auto refresh the timeout for each user if they have an active session.
Below is a little code I found and have been playing with, I would be grateful for any ideas or example anyone could share of how I could best achieve this.
Many thanks
Andy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Exchange.WebServices.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Url = new Uri("https://server/EWS/Exchange.asmx");
service.Credentials = new NetworkCredential("user", "pass", "domain");
SetStreamingNotifications(service);
while (true)
{ }
}
static void SetStreamingNotifications(ExchangeService service)
{
// Subscribe to streaming notifications on the Inbox folder, and listen
// for "NewMail", "Created", and "Deleted" events.
StreamingSubscription streamingsubscription = service.SubscribeToStreamingNotifications(
new FolderId[] { WellKnownFolderName.Calendar, WellKnownFolderName.Inbox },
EventType.Created,
EventType.Modified);
StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 9);
connection.AddSubscription(streamingsubscription);
// Delegate event handlers.
connection.OnNotificationEvent +=
new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
connection.OnSubscriptionError +=
new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
connection.OnDisconnect +=
new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
connection.Open();
Console.WriteLine("--------- StreamSubscription event -------");
}
static private void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
{
// Cast the sender as a StreamingSubscriptionConnection object.
StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection)sender;
// Ask the user if they want to reconnect or close the subscription.
ConsoleKeyInfo cki;
Console.WriteLine("The connection to the subscription is disconnected.");
Console.WriteLine("Do you want to reconnect to the subscription? Y/N");
while (true)
{
cki = Console.ReadKey(true);
{
if (cki.Key == ConsoleKey.Y)
{
connection.Open();
Console.WriteLine("Connection open.");
break;
}
else if (cki.Key == ConsoleKey.N)
{
// The ReadKey in the Main() consumes the E.
Console.WriteLine("\n\nPress E to exit");
break;
}
}
}
}
static void OnEvent(object sender, NotificationEventArgs args)
{
StreamingSubscription subscription = args.Subscription;
// Loop through all item-related events.
foreach (NotificationEvent notification in args.Events)
{
switch (notification.EventType)
{
case EventType.NewMail:
Console.WriteLine("\n-------------Mail created:-------------");
break;
case EventType.Created:
Console.WriteLine("\n-------------Item or folder created:-------------");
break;
case EventType.Deleted:
Console.WriteLine("\n-------------Item or folder deleted:-------------");
break;
}
// Display the notification identifier.
if (notification is ItemEvent)
{
// The NotificationEvent for an e-mail message is an ItemEvent.
ItemEvent itemEvent = (ItemEvent)notification;
Console.WriteLine("\nItemId: " + itemEvent.ItemId.UniqueId);
}
else
{
// The NotificationEvent for a folder is an FolderEvent.
//FolderEvent folderEvent = (FolderEvent)notification;
//Console.WriteLine("\nFolderId: " + folderEvent.FolderId.UniqueId);
}
}
}
static void OnError(object sender, SubscriptionErrorEventArgs args)
{
// Handle error conditions.
Exception e = args.Exception;
Console.WriteLine("\n-------------Error ---" + e.Message + "-------------");
}
}
}
The way I solved this problem is by:
Having an account that has the right to impersonate all users.
I create a service for that account with giving a username and
password.
I impersonate a user and add the subscription of the user to the
connection
I create another service which is a close for the main service with
the same username and password, which will impersonate another user
and then add the subscription to the same connection before
Here are two parts of my code . Forget about the LogDevice it is just something internally.
The first part is the detailed impersonation and adding the service to the list of services
the list of services in my case is a dictionary with the userSMTP is the key , the UserSMTP key here is the impersonated account.
/// <summary>
/// Impersonate one user at a time and without using the autodiscovery method to find the proper url for the userSmtp,
/// and copy the provided url to the usersmtp.
/// </summary>
/// <param name="url"> </param>
/// <param name="userSmtp">user smtp</param>
/// <param name="enableTrace">to enable logging from the XML tracing </param>
/// <param name="exchangeVersion">Exchange server version used</param>
private Uri ImpersonateUser(Uri url, string userSmtp, bool enableTrace, ExchangeVersion exchangeVersion)
{
Uri result = url;
var log = "ImpersonateUser \n";
try
{
log += "0/8 Checking services redundancy\n";
if (Services.ContainsKey(userSmtp))
{
Services.Remove(userSmtp);
}
log += "1/8 Create a new service for " + userSmtp + "\n";
var service = new ExchangeService(exchangeVersion);
log += "2/8 Get credentials for the service\n";
var serviceCred = ((System.Net.NetworkCredential)(((WebCredentials)(Services.First().Value.Credentials)).Credentials));
log += "3/8 Assign credentials to the new service\n";
service.Credentials = new WebCredentials(serviceCred.UserName, serviceCred.Password);
log += "4/8 TraceEnabled is" + enableTrace.ToString() + "\n";
service.TraceEnabled = enableTrace;
log += "5/8 Get the Url for the service with AutodiscoverUrl \n";
service.Url = url;
log += "6/8 Assign a new ImpersonatedUserId to the new service for" + userSmtp + "\n";
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, userSmtp);
try
{
log += "7/8 Validating the impersonation\n";
RuleCollection rulecoll = service.GetInboxRules();
}
catch (Exception ex)
{
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUser: failed to validate the impersonation for {0}\n Exception: {1}\n", userSmtp, ex.Message);
int hr = System.Runtime.InteropServices.Marshal.GetHRForException(ex);
if (hr == -2146233088) // We do not have right to impersonate this user.
{
result = null;
return result;
}
else
{
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUser(2): trying to resolve {0} with Autodiscover instead...", userSmtp);
result = ImpersonateUser(userSmtp, enableTrace, exchangeVersion);
}
}
log += "8/8 Adding the service \n";
if (!Services.ContainsKey(userSmtp))
{
Services.Add(userSmtp, service);
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUser(2): {0} has been impersonated\n", service.ImpersonatedUserId.Id);
}
}
catch (Exception ex)
{
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUser(2): exception {0}\n The exception occured after the following steps: \n{1}", ex.Message, log);
}
return result;
}
And here is the code that calls the previous function (i.e. for all users) put in mind that you should somehow storing the email address for every account you want to impersonate.
/// <summary>
/// To Impersonate users in order to get the info from them.
/// </summary>
/// <param name="userSmtps">List of users to be impersonated</param>
/// <param name="enableTrace"> To enable logging from the XML tracing</param>
/// <param name="exchangeVersion">Exchange server version used </param>
public void ImpersonateUsers(ICollection<string> userSmtps)
{
var log = "ImpersonateUsers\n";
var firstUserSmtp = "";
if (userSmtps != null)
if (userSmtps.Count > 0)
{
//the url for the first smtp
try
{
log += "1/2 Impersonating the first userSmtp\n";
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUsers: Getting the Url from the autodiscovery for the first smtp {0} ", userSmtps.First());
bool enableTrace = Services.First().Value.TraceEnabled;
ExchangeVersion exchangeVersion = Services.First().Value.RequestedServerVersion;
firstUserSmtp = userSmtps.First();
var commonSmtpUrl = ImpersonateUser(userSmtps.First(), enableTrace, exchangeVersion);
if (commonSmtpUrl == null) userSmtps.Remove(firstUserSmtp);
// If the list contains other than the first one
log += "2/2 Impersonating " + (userSmtps.Count - 1) + " userSmtps\n";
if (userSmtps.Count >= 1)
{
foreach (var userSmtp in userSmtps)
{
try
{ //skip ther first one because it is already impersonated.
if (userSmtp == firstUserSmtp)
{
continue;
}
// Impersonate the users
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUsers: Impersonating {0} ...", userSmtp);
commonSmtpUrl = ImpersonateUser(userSmtp, enableTrace, exchangeVersion);
}
catch (Exception ex)
{
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUsers: Impersonating {1}\n exception {0}\n", ex.Message, userSmtp);
}
}
}
}
catch (Exception ex)
{
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUsers: exception {0}\n The exception occured after the following steps: \n{1}", ex.Message, log);
}
}
}
I would have put the subscription part and adding to the connection , but it is a bit ugly and hard to get. but the idea is simply that you should have a connection, and then you go to each service you made and then `connection.AddSubscription(streamingSubscription);
Where streamingSubscription is extracted from the service.

Resources