How to get the device token - android

After the installation has been made I need to get the deviceToken for other purposes. This is what I developed so far:
Parse.initialize(this, "qqd423WEfwWEF32FewferT434fs323rfRT", "g7Rre4g7gsGRwgGw458Hdf443gFHk534Mrtg34");
final ParseInstallation currentInstallation = ParseInstallation.getCurrentInstallation();
currentInstallation.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
System.out.println("ok");
deviceToken = currentInstallation.get("deviceToken").toString();
System.out.println(deviceToken);
} else {
System.out.println("not ok");
}
}
});
The problem is that if I execute the code the app crashes and this is the error generated:
02-02 09:44:17.151 ﹕ FATAL EXCEPTION: main, PID: 5855 java.lang.NullPointerException
and the piece of code that generates this is this piece:
deviceToken = currentInstallation.get("deviceToken").toString();
is there anybody who can help me? I simply need to get the deviceToken after the installation is made.

There is no problem in code, I guess you are testing this code in emulator and in emulator it will not have any deviceToken, try your code in real device.

Might be Late but also might be helpfull :)
Parse sometimes has some delays to when the done() method is called, I've seen it being called after I played around with the application for 3 minutes and exit it.
I used a different order for the method calls:
Parse.initialize(this, "*******", "*******");
ParsePush.subscribeInBackground("", new SaveCallback()
{
#Override
public void done(ParseException e)
{
if (null == e)
{
ParseInstallation install = ParseInstallation.getCurrentInstallation();
String token = install.getString("deviceToken");
if (token != null)
{
//saved it locally and other stuff
}
else
{
//saved a temporary default value locally
}
}
}
});
ParseInstallation.getCurrentInstallation().saveInBackground();
Hope this Helps in some way.

Related

ParseRequestException (#ParseRequest:newPermanentException:352) {main}

I am getting the following exception reported in my Google Analytics for Android app.
ParseRequestException (#ParseRequest:newPermanentException:352) {main}
This is the top exception with more than 4000 hits. I am worried that some users are not able to register on parse and unable to receive push notifications. Unfortunately, I don't have any other information being reported and I am unable to replicate this on any Android devices that I have.
Here is the code, where I am initializing Parse:
Parse.initialize(this, "XXX", "YYY");
ParseInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
Utils.sendException(appContext, e);
}
}
});
PushService.setDefaultPushCallback(this, ChatActivity.class);
ParsePush.subscribeInBackground(getResources().getString(R.string.parse_default_channel), new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
Toast.makeText(appContext, "Problem subscribing to the chat service. You will not be able to receive notifications!", Toast.LENGTH_LONG).show();
Utils.sendException(appContext, e);
}
}
});
When I searched for this Exception on Google, I found only 3 results and they don't have any info. I want to know:
1. If I am doing something wrong?
2. How this may be hurting my app's users?
3. How to solve this?
Thanks in advance!

Parse.com Only one device session for a user. Android

I'm currently building an app that has Parse at its backend. I've realized that a user can log in to his account in multiple devices. Is there a way I could restrict this to only one device at one time? If so can you kindly explain the method.
Thanks in advance.
you need to monitor the application will your web environment (webservice for example) As soon as someone you login you must disconnect the other devices connected to the same user.
You can analyze it by IMEI who made the last login request and send a command to the other devices of the same user to remove access.
I came here looking for a solution and didn't find one. Through trial and error, I have solved it. I am a novice developer and do not know if this is the best practice, but it works. When the user attempts to log in execute the following.
public void login(View v) {
// Create string variables for data.
String username = et_username.getText().toString().toLowerCase().trim();
String password = et_password.getText().toString();
// If a user is already on this device, log them out.
// this will happen when the force log out happens, the device will
// simply catch the invalid session, but still have that user data, just unavailable
user = ParseUser.getCurrentUser();
if (user != null)
user.logOutInBackground();
ParseUser.logInInBackground(username, password, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
final ParseQuery<ParseObject> query = new ParseQuery<>("_Session");
query.include("user");
query.whereEqualTo("user", ParseUser.getCurrentUser()); // Only get sessions for the specific user.
query.addAscendingOrder("createdAt"); // Place them in chronological order, with the oldest at the top.
query.countInBackground(new CountCallback() {
#Override
public void done(int count, ParseException e) {
if (count > 1) {
try {
query.getFirst().deleteInBackground();
} catch (ParseException e1) {
Toast.makeText(LoginActivity.this, e1.toString(), Toast.LENGTH_LONG).show();
}
}
}
});
if (user != null) {
emailVerified = user.getBoolean("emailVerified");
if (emailVerified) {
// Update the database to track their log in.
user.put("loggedIn", true);
user.saveInBackground();
if (!deliquent) {
// Launch the MainActivity.
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
else {
Intent intent = new Intent(LoginActivity.this, CardActivity.class);
startActivity(intent);
}
}
else {
Toast.makeText(LoginActivity.this, getResources().getString(R.string.verify), Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(LoginActivity.this, "From LoginActivity line:213\n" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
}

parse - notifications are not received on the first time

I'm using Parse Android SDK v 1.9.2. Notifications are not received for the first time. But if I reinstall the App, then notifications are received.
My ApplicaitonClass
#Override
public void onCreate() {
super.onCreate();
......
Parse.initialize(this, "xxx", "xxx");
.....
}
I'm registering few additional attributes post login and doing saveInBackground ..... onCreate method
The activity is getting called and i'm seeing the logs before saveinbackground and no error on savecallback too.
.saveEventually(new SaveCallback() {
#Override
public void done(ParseException e) {
System.out.println("Done");
if (e == null) {
System.out.println("Parse --- Succesfull Registration.....");
} else {
System.out.println("Parse --- " + e);
}
}
});
But still it doesnt work on the first time, even after closing the app and starting again. However, it works if i reinstall the app.
Kindly let me know what am i doing wrongly.
After trying various examples and debugging, I finally found what i was doing wrongly. I was calling remove key from the parse defaultInstallation in order to avoid the duplicates or array.
So i need to check whether the key exists or not.
if(ParseInstallation.getCurrentInstallation().get("secretId") != null && ParseInstallation.getCurrentInstallation().get("secretId").toString().length() > 0) {
ParseInstallation.getCurrentInstallation().remove("secretId");
}

How to set value of a column of installation object of parse in android

I'm trying to learn how to use parse and at this moment I want to know how I can set the value of a column at initialization ("Installation" object).
This is my onCreate method.
public void onCreate() {
super.onCreate();
Parse.initialize(this, "****", "****");
ParseInstallation.getCurrentInstallation().saveInBackground();
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
} else {
Log.e("com.parse.push", "failed to subscribe for push", e);
}
}
});
}
I added a column called MacID. Whenever someone downloads the app, I want to get their mac address and save it to parse object. Thank you
For the installation object
First go to the parse website and create the required column in the installation class.
Then in code
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("column name",mcIdValue);
installation.saveInBackground();

Parse.com android - disable push notifications

I've started using parse.com to receive push notifications in my application.
It works perfectly, but I have a couple of questions.
We perform registration, but unused channels, as follows:
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("dmode", "Oks!");
} else {
Log.e("dmode", "Fail :(", e);
}
}
});
My first question is:
I find no way to disable receiving push notifications
I searched the official documentation and stackoverflow, but I find solutions that do not work for me.
I tried:
ParsePush.unsubscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("dmode", "unsuscribe oks");
} else {
Log.e("dmode", "unsuscribe fail", e);
}
}
});
Unsuccessfully, I have also tried:
ParsePush.unsubscribeInBackground("");
...but not work.
I'm not using channels, that is why the quotes are empty:
ParsePush.subscribeInBackground("", new SaveCallback() { //...
How I can enable and disable push notifications?
The second query is that sometimes receive duplicate notifications until two three times at once on the same device.
Someone has been the same?
Thank you very much for the help.
Greetings!
I think you should use a name for a default channel even if you are not using channels and not the empty string "".
Also have a look here Parse Question
I quote from the answer on the above link
If you called PushService.setDefaultCallback, call it again passing null as the class.

Categories

Resources