keen
keen

Reputation: 3011

Not receiving notification

We are using parse to store chat messages and we are using notification of parse.

For iOS we are doing this to create entry in installation table of parse.. It's creating entry in installation table of parse, which i think is must to receive notification.

 PFInstallation *currentInstallation = [PFInstallation currentInstallation];

 currentInstallation.deviceToken = user.notificationToken;

[currentInstallation saveInBackground];

But in android I am not able to create entry in installation table.

I am not receiving notification from parse and I think this is the reason behind not getting notification..

Is this the case?

Can any one guild me to the right path ?

UPDATE

Now receiving notification but still having some doubts. I am doing this when user logs in. I have put conditions if user is not created then only create otherwise don't. I haven't added it to this because that's not necessary

   // setting up parse installation
private void setUpParseInstallation()
{

    ParseInstallation currentInstallation = ParseInstallation
            .getCurrentInstallation();

    currentInstallation.saveInBackground(new SaveCallback()
    {

        @Override
        public void done(com.parse.ParseException e)
        {
            if (e != null)
            {
                Log.e(TAG, "Error saving parse installation");
            }
        }
    });
}




// add user to parse it it doesn't exist otherwise handle the cases
private void addUserToParseIfNotExisting()
{

            // if user doesn't exist create a new user
        ParseObject newuser = new ParseObject("user");
            newuser.put("name", email);
            newuser.put("userId", id);

            newuser.saveInBackground();
}


private void setChannel()
{
     channel = "abdf";
         RS_User user = new RS_User(this);

    ParseQuery<ParseObject> pquery = ParseQuery.getQuery("user");

    pquery.whereEqualTo("userId", user.getUserId());

    // see if user exists in user table on parse
    pquery.findInBackground(new FindCallback<ParseObject>()
    {

        @Override
        public void done(List<ParseObject> list,
                com.parse.ParseException error)
        {

            if (list.size() > 0)
            {
                        list.get(i).put("channels", channel);
                        list.get(i).saveInBackground();
                                            PushService.subscribe(getApplicationContext(),channel, RS_HelpActivity.class);

            }
       } 
   });
}

Sending a notification

ParsePush push = new ParsePush();
                                if(object.get(k).has("channels"))
                                {
                                    push.setChannel(object.get(k).getString(
                                            "channels"));
                                }
                                push.setData(dataAndroid);
                                push.sendInBackground();

Currently I am doing all this to send/receive notification using Parse. Is there a way to do without using channel and directly using notification token or something else using parse ? Because I will be having user notification token.

Upvotes: 3

Views: 1273

Answers (2)

Darshak
Darshak

Reputation: 2316

I hope this code will help you

Application.java

public class Application extends android.app.Application {

  public Application() {
  }

  @Override
  public void onCreate() {
    super.onCreate();

    // Initialize the Parse SDK. 
    Parse.initialize(this, "your applicationId", "your clientKey"); 

    // Specify an Activity to handle all pushes by default.
    PushService.setDefaultPushCallback(this, MainActivity.class);
  }
}

AndroidManifest.xml

<application
    android:name="<Your Package Name>.Application"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

Upvotes: 5

Luca Sepe
Luca Sepe

Reputation: 2445

Have you tried to use advanced targeting to send a notification to any installation with a device token which matches a contained in (in) query (or any other condition you need).

Something like this:

 ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
 query.whereEqualTo("your_key", "your_value");
 // OR any of whereXXX method

 ParsePush push = new ParsePush();
 push.setData(dataAndroid);
 push.setQuery(query);
 push.sendInBackground();

EDIT

ParseInstallation as others ParseObject(s) has put method.

Since in Android you haven't a UDID like iOS you have to figure out how to create something similiar. Unfortunately I cannot send you how we generate "ours" device token, but here you can find valid suggestions.

  ParseInstallation install = ParseInstallation.getCurrentInstallation();
  install.put("device-token", yourGeneratedDeviceId);
  install.saveInBackground(new SaveCallback() { ... });

Upvotes: 0

Related Questions