Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send some additional data while sending push notifications using OneSignal?

I am developing an Android application as well as writing writing a C# Web Api for it. Now i can send push notifications using the code below. But i have to send a json object which will contain a url for an image so that when user clicks the notification , an activity in the app opens and using that url loads the image using Picasso. How should i do it?

private void SendPushNotifications(int userId)
    {
        string appId = "myAppId";
        var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest;
        var user = db.Users.FirstOrDefault(x => x.Id == userId);
        if (user != null)
        {
            string message = "This job is posted by: \n" + user.Name + "\n" + user.Contact + "\n" +user.City;
            if (request != null)
            {
                request.KeepAlive = true;
                request.Method = "POST";
                request.ContentType = "application/json";

                request.Headers.Add("authorization", "Basic "+appId);

                byte[] byteArray = Encoding.UTF8.GetBytes("{"
                                                          + "\"app_id\": \"app_id\","
                                                          + "\"contents\": {\"en\": \""+ message +"\"},"
                                                          + "\"included_segments\": [\"All\"]}");

                string responseContent = null;

                try
                {
                    using (var writer = request.GetRequestStream())
                    {
                        writer.Write(byteArray, 0, byteArray.Length);
                    }

                    using (var response = request.GetResponse() as HttpWebResponse)
                    {
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            responseContent = reader.ReadToEnd();
                        }
                    }
                }
                catch (WebException ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    System.Diagnostics.Debug.WriteLine(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
                }

                if (responseContent != null) System.Diagnostics.Debug.WriteLine(responseContent);
            }
        }
    }

with this "message" string i also want to send a json object.

like image 941
Harry .Naeem Avatar asked Jun 06 '16 10:06

Harry .Naeem


People also ask

How do I send push notifications to OneSignal?

For sending notification in One Signal click on your app name on the top left corner in One Signal Console and then click on your app name you will get to see the Dashboard screen. Click on the New Push option to send a new notification.

What is push notification payload?

Push payloads are sent with encrypted content when the mobile app supplies an RSA public encryption key upon push registration with the Salesforce push notification service. When a payload is sent from a customer org to a user's device, the mobile app processes and decrypts the payload.

How many push notifications should I send?

As a rule of thumb, do not send more than 3-5 notifications every week. Even then, make sure that the notifications have something relevant for the user. Understand how users would use your app in their daily lives and identify opportunities to enhance that experience using push messages.


1 Answers

So, I found the solution to this problem.

Solution: OneSignal allows to send additionalData by using 'data' tag in the encoded string that i'm sending like below:

byte[] byteArray = Encoding.UTF8.GetBytes("{"
             + "\"app_id\": \"app_id\","
             + "\"data\": {\"foo\": \"bear\"},"
             + "\"contents\": {\"en\": \"" + message + "\"},"
             + "\"included_segments\": [\"All\"]}");

So in Android it will be mapped on JsonObject additionalData

OneSignal.startInit(this)
        .setNotificationOpenedHandler(new OneSignal.NotificationOpenedHandler() {
            @Override
            public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
                Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();
            }
        })
        .init();

And you can easily use it. :)

like image 76
Harry .Naeem Avatar answered Sep 16 '22 11:09

Harry .Naeem