I am trying to implement parse push notification in my android project. to understand, I am giving some project flow.
saving user details on personal server using php API
server send me some push notification.
I get notification but I am not getting any action, data in that push notification in my custom broadcast receiver.
Here is my Android Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CAMERA" />
<permission
android:name="com.mypackage.myapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mypackage.myapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:name="com.mypackage.myappe.MyApplication"
android:allowBackup="true"
android:icon="#drawable/dwtlogo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:name="com.parse.PushService" />
<receiver
android:name="com.mypackage.myapp.MyCustomReceiver"
android:exported="false" android:enabled="true">
<intent-filter>
<action android:name="com.mypackage.myapp.MESSAGE" />
</intent-filter>
</receiver>
<category android:name="com.mypackage.myapp" />
</application>
Here is MyApplication.java
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "xxxxxxxx", "xxxxxxxxxx");
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
PushService.setDefaultPushCallback(getApplicationContext(), DashBoardActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseUser.enableAutomaticUser();
//String deviceToken= (String) installation.get("deviceToken");
//Toast.makeText(this, "Device token="+deviceToken, Toast.LENGTH_LONG).show();
PushService.subscribe(this, "Wake_up", DashBoardActivity.class);
}
And here is my custom receiver in which i am trying to get action and data which server sending me but my custom receiver not getting called.
String action = intent.getAction();
Log.d(TAG, "got action " + action );
if (action.equals("com.mypackage.myapp.MESSAGE"))
{
String channel = intent.getExtras().getString("com.parse.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
Iterator itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
//if (key.equals("customdata"))
//{
Intent pupInt = new Intent(context, ShowPopup.class);
pupInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.getApplicationContext().startActivity(pupInt);
//}
Log.d(TAG, "..." + key + " => " + json.getString(key));
}
}
}
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
Even no any log getting printed. Here is php server code which sending me push notification.
$data = array(
'channel' => $chnl,
'action' => 'com.mypackage.myapp.MESSAGE',
'data' => array(
'alert' => $message,
'sound' => 'push.caf',
'photourl' => $imageurl,
)
);
But i don't understand where i need to get this data so i can show image after receiving push notification.
Please give me any hint or reference.
Adding receiver for GcmBroadcastReceiver should help. As given out in the tutorial here.
Other reference.
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.tutorials.pushnotifications" to match your app's package name.
-->
<category android:name="com.parse.tutorials.pushnotifications" />
</intent-filter>
</receiver>
Related
I want to send push Notification using Parse API and GCM. I have done successfully configuration on server and test via sending push notification form Parse terminal and receive at android device.
But when i'm sending push programmatically then got an Exception: "unauthorized: master key is required".
I using following code:
ParseQuery query = ParseInstallation.getQuery();
query.whereEqualTo("channels", "testing");
// Notification for Android users
query.whereEqualTo("deviceType", "android");
ParsePush androidPush = new ParsePush();
androidPush.setMessage("Your suitcase has been filled with tiny robots!");
androidPush.setQuery(query);
androidPush.sendInBackground(new SendCallback() {
#Override
public void done(ParseException e) {
if ( e == null ) {
Log.d("GARG","Parse Notification Done. : ");
} else {
Log.d("GARG","Notification failed.: "+e.getMessage());
e.printStackTrace();
}
}
});
android manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo">
<permission
android:name="com.demo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.demo.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.demo.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.demo.permission.C2D_MESSAGE" />
<application
android:name=".AppInitializer"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<service android:name="com.parse.PushService" />
<meta-data
android:name="com.parse.push.notification_icon"
android:resource="#drawable/ic_cast_light" />
<meta-data android:name="com.parse.push.gcm_sender_id"
android:value="id:211671060483" />
<meta-data
android:name="com.parse.APPLICATION_ID"
android:value="#string/parse_app_id" />
<meta-data
android:name="com.parse.CLIENT_KEY"
android:value="#string/parse_client_key" />
<meta-data
android:name="com.parse.X-Parse-Master-Key"
android:value="#string/parse_master_key" />
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.demo" />
</intent-filter>
</receiver>
</application>
Initialize Parse SDK in android
public class AppInitializer extends Application {
public static JSONObject errorDataProvider;
#Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(getApplicationContext());
Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
.applicationId(Utility.ApplicationId)
.clientKey(Utility.ClientKey)
.server(Utility.Server_URL)
.build()
);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParsePush.subscribeInBackground(Utility.PARSE_CHANNEL, new SaveCallback() {
#Override
public void done(ParseException e) {
Log.e("GARG", "Successfully subscribed to Parse!");
}
});
}
}
UPDATED ANSWER
Hi, i investigate it a bit and found that currently the only to send Push is by using the masterKey and that's exactly the reason why you are getting this error
In order to send push with master key the best approach will be to create a cloud code function and trigger this function from the client side.
so you need to do the following:
Inside your cloud code main.js file create a new function
Parse.Cloud.afterSave("SendPush", function(request) {
var query = new Parse.Query(Parse.Installation);
query.exists("deviceToken");
// here you can add other conditions e.g. to send a push to sepcific users or channel etc.
var payload = {
alert: "YOUR_MESSAGE"
// you can add other stuff here...
};
Parse.Push.send({
data: payload,
where: query
}, {
useMasterKey: true
})
.then(function() {
response.success("Push Sent!");
}, function(error) {
response.error("Error while trying to send push " + error.message);
});
});
After you created the cloud code function restart your server
Trigger this cloud code function from your android app in the following way:
HashMap<String,String> map = new HashMap<String, String>();
map.put("PARAM1KEY","PARAM1VALUE");
// here you can send parameters to your cloud code functions
// such parameters can be the channel name, array of users to send a push to and more...
ParseCloud.callFunctionInBackground("SendPush",map, new FunctionCallback<Object>() {
#Override
public void done(Object object, ParseException e) {
// handle callback
}
});
This will trigger a call to the cloud code function that you created above and inside the cloud code the useMasterKey is true so it should work.
update: spelling
useMasterKey: true
that will enable parse server to use master key to push notification from client side.
I can receive message google-cloud-messaging data payload when my app is active, but when the app is not active I do not receive messages.
My device is android, when app is alive,send notification or data message. It worked.
but when app is not alive, sent data message, it doesn't work.
Is there any way to use the data payload message to activate the intent and receive the message when my app is not alive?
there is my code, according to the demo:
public class AgooGcmListenerService extends GcmListenerService{
public final static String TAG = "AgooGcmListenerService";
private AgooFactory agooFactory;
#Override
public void onMessageSent(String msgId) {
super.onMessageSent(msgId);
}
#Override
public void onSendError(String msgId, String error) {
ALog.e(TAG, "onSendError,msgId=" + msgId + ",error=" + error);
super.onSendError(msgId, error);
}
#Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
ALog.d(TAG, "From: " + from);
ALog.d(TAG, "Message: " + data.toString());
if(TextUtils.isEmpty(message)){
return;
}
byte[] msg = message.getBytes();
Context mContext = getApplicationContext();
agooFactory = new AgooFactory();
agooFactory.init(mContext, null, null);
agooFactory.msgRecevie(msg,
AgooConstants.MESSAGE_SYSTEM_SOURCE_GCM);
Intent intent = new Intent();
intent.setAction("org.agoo.android.intent.action.PING_V4");
intent.setClassName(mContext.getPackageName(),ProxyFactroy.getChannelService(mContext));
intent.putExtra("source", "accs-bundle");
mContext.startService(intent);
}
}
there is my config:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<service
android:name="org.android.agoo.thirdPush.AgooGcmListenerService"
android:process=":channel"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [START gcm_receiver] -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:process=":channel"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.taobao.taobao" />
</intent-filter>
</receiver>
<service
android:name="org.android.agoo.thirdPush.AgooInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
here i am really not getting why my code is not sending push from a user to another user ,i can send and receive pushes by sending through channels but problem is i get push notification to all user, not the specific user ,how can i send from a user to another user here below is my code please explain me what is my mistake ,or how to me it work
MY onClick of button where things must happen
ok.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
final String currentUserId = ParseUser.getCurrentUser().getObjectId();
ParseQuery<ParseUser> query = ParseUser.getQuery();
getPhone = phone.getText().toString();
//for not including myself
query.whereNotEqualTo("objectId", currentUserId);
query.whereEqualTo("username", getPhone);
query.getFirstInBackground(new GetCallback<ParseUser>()
{
public void done(final ParseUser user, ParseException e)
{
if (user == null)
{
Toast.makeText(Welcome.this, "couldnot connect to " + getPhone, Toast.LENGTH_SHORT).show();
Log.d("username", "problem retriving username");
}
else
{
ParseQuery pushQuery = ParseInstallation.getQuery();
pushQuery.whereEqualTo("email", "three");
final String name = user.getUsername();
String data = "{\n" +
"\"data\":{\n "+
"\"message\":\"Connection request\",\n" +
"\"title\":\"Connection\",\n" +
"\"from\":"+"\""+ParseUser.getCurrentUser().getUsername()+"\""+"\n "+
"}\n" +
"}";
JSONObject jsonObject = null ;
try
{
jsonObject = new JSONObject(data);
} catch (JSONException e1)
{
e1.printStackTrace();
}
ParsePush push = new ParsePush();
push.setQuery(pushQuery);
push.setData(jsonObject);
//push.setChannel("Giants");
push.sendInBackground(new SendCallback()
{
#Override
public void done(ParseException e)
{
if (e == null)
{
Toast.makeText(getApplicationContext(), "request send to "+name, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "problem sending request to "+name+" due to "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
);
Intent i = new Intent(Welcome.this, TestActivity.class);
Log.e("about user", "connected to " + user.getUsername());
retrivedUser = user.getUsername();
i.putExtra("number", retrivedUser);
startActivity(i);
}
}
});
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.parse.starter.permission.C2D_MESSAGE" />
<uses-permission android:name="com.example.chattapp.permission.C2D_MESSAGE" />
<application
android:name=".Chattapp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name" />
<activity android:name=".DispatchActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity" />
<activity android:name=".SignUpActivity" />
<activity android:name=".Welcome" />
<activity android:name=".TestActivity"></activity>
<service android:name="com.parse.PushService" />
<!--<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>-->
<receiver android:name="com.example.chattapp.CustomReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--IMPORTANT: Change "com.parse.starter" to match your app's package name.-->
<category android:name="com.example.chattapp" />
</intent-filter>
</receiver>
</application>
here is the push image
push details
This is what I did for a chat app I made, every time a user sings up on your app, suscribe it to a channel with their user id. That way you will have an unique channel for every user and you can send individual pushes to them.
Here is a quick code I put up:
ParseUser user = new ParseUser();
user.setUsername("my name");
user.setPassword("my pass");
user.setEmail("email#example.com");
// other fields can be set just like with ParseObject
user.put("phone", "650-253-0000");
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
String userID = user.getObjectId();
ParsePush.subscribeInBackground(userID});
} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
}
}
});
After this you just need the userid of the receiver and that's it.
Is client push enabled in the parse application settings ?
It's disabled by default.
More infomation can be found at the parse blog:
http://blog.parse.com/learn/engineering/the-dangerous-world-of-client-push/
I am trying to get started with Parse push notification on Android, but I could not find any way to implement my own BroadcastReceiver which would perform action in response to a push notification.
In their "getting started", they give a program sample where the receiver and the intent service is contained in the jar file (and not implemented in the code, so that I could try and play with it). A similar question has been asked here, but it's two years old and the answer has 11 downvotes.
There are a few other blog posts, e.g. as in here, but none of them work properly (the one in that link was two years old).
So please kindly suggest a step by step way to implement the same.
EDIT:
I tried the code as suggested by droidx. But it does not work. I am sending my AndroidManifest.xml (rest of the code is exactly as he suggested):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rahulserver.parsenotificationdemo" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.rahulserver.parsenotificationdemo.permission.C2D_MESSAGE" />
<uses-permission android:name="com.rahulserver.parsenotificationdemo.permission.C2D_MESSAGE" />
<application
android:name=".ParseNotification"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name=".MyCustomParsePushReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.starter" to match your app's package name.
-->
<category android:name="com.rahulserver.parsenotificationdemo" />
</intent-filter>
</receiver>
</application>
</manifest>
Here's an example!
public class ParseCustomBroadcastReceiver extends ParsePushBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, json.getString("alert").toString());
final String notificationTitle = json.getString("title").toString();
final String notificationContent = json.getString("alert").toString();
final String uri = json.getString("uri");
Intent resultIntent = null;
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
resultIntent = new Intent(context, HomeScreen.class);
stackBuilder.addParentStack(HomeScreen.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
//Customize your notification - sample code
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_notification_icon)
.setContentTitle(notificationTitle)
.setContentText(notificationContent);
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, builder.build());
} catch (JSONException e) {
Log.d(TAG, e.getMessage());
}
}
}
Following this guide, replace android name for the receiver to the path of your custom broadcastreceiver
<receiver
android:name=".receivers.ParseCustomBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
In your application class, make sure to have subscribed to Parse Push like this
Parse.initialize(this, ParseUtils.PARSE_APPLICATION_ID_DEV, ParseUtils.PARSE_CLIENT_KEY_DEV);
ParseObject.registerSubclass(Article.class);
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d(TAG, "successfully subscribed to the broadcast channel.");
} else {
Log.e(TAG, "failed to subscribe for push", e);
}
}
});
Update:
The above broadcast receiver expects to send a json object notification from parse dashboard usually consists of three name value pairs:
{ "alert": "Alert message", "title": "Title", "uri": "bla bla" }
When you use only plain string for eg: hello world! in parse push dashboard, internally it goes as only alert tag. something like this : {"alert" : "hello world!, "title" = null/"", "uri" = null/""}
The broadcast receiver code above doesn't have this null checks, so it's caught in the catch with exception No title/uri.
So you can add some null checks based on what you are expecting from the push notification.
Something like this:
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, json.getString("alert").toString());
if (json.has("title")) {
notificationTitle = json.getString("title").toString();
}
if (json.has("alert")) {
notificationAlert = json.getString("alert").toString();
}
if(json.has("uri")) {
notificationURI = json.getString("uri");
}
Then set appropriate contentTitle and contentText in notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notificationAlert)
.setContentText(notificationAlert);
Hope this helps!
I am developing simple android app and very new to parse. I followed parse documentation in adding back-end functionalities. Parse CORE was an easy part but I can’t send push notifications from android device. But push-notifications are received when sent from parse dashboard.
My Manifest file is as below:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.google.android.c2dm.permission.SEND" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="loaniistar.loaniistar.permission.C2D_MESSAGE" />
<uses-permission android:name="loaniistar.loaniistar.permission.C2D_MESSAGE" />
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.starter" to match your app's package name.
-->
<category android:name="loaniistar.loaniistar" />
</intent-filter>
</receiver>
Application Class:
public class MApplication extends Application {
String applicationiId = "5npdddECsGdDk49OttttuHq6iZdZpddI0cHDsz";
String clientKey = "SjF1BWamssstycthSjf2dddduhcuwW2VccccCCuFlE";
public UserAccounts userAccount = null;
#Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
ParseCrashReporting.enable(this);
Parse.initialize(this, applicationiId, clientKey);
PushService.startServiceIfRequired(this);
}
}
Subscribing for channel in an activity class:
ParsePush.subscribeInBackground("manager", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
}
else
{
btnLoginEnable();
}
}
});
Sending Push Notification in another activity class:
// Sending Push
ParsePush push = new ParsePush();
push.setChannel("manager");
push.setMessage(m.getMessage().substring(0, (int) m.getMessage().length() / 3));
//push.sendInBackground();
push.sendInBackground(new SendCallback() {
#Override
public void done(ParseException e) {
if(e == null)
{
Toast.makeText(getActivity(), "Notification sent", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getActivity(), "Notification Not sent", Toast.LENGTH_SHORT).show();
}
}
});
Row is added in “Installation” class and GCMSenderId is empty!! Is this the issue?
Please help me out why I can’t receive notifications when sent from my android device?
Libs used:
Bolts-android-1.1.4.jar
Parse-1.8.2.jar
ParseCrashReporting-1.8.2.jar
If you have just created the App in Parse and integrated in your App, It generally not works immediately Same thing happened to me. I was not getting the data, but after some time I got the requests when I saw the Parse Dashboard
It will take Approx 20 mins. You can try to see the Parse Analytics.(if you have implemented it) if Parse shows Requests in your Analytics Dashboard then It will work completely fine