I am using PushService.subscribe(context,channel,NotificationActivity.class) to subscribe to the channel to get the push notification. I am able to get the push notification but not able to get the additional data in NotificationActivity. Here is my code.
JSONObject data = new JSONObject();
data.put("action","com.example.myapp.UPDATE_STATUS")
data.put("postTitle",result.get(0).getTitle() );
data.put("description", result.get(0).getDescription());
ParsePush push = new ParsePush();
push.setChannel(tag);
push.setData(data);
push.setMessage("Meassge sent is"+tag);
push.send();
NotificationActivity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
ParseAnalytics.trackAppOpened(getIntent());
Intent intent = getIntent();
Bundle extras = intent.getExtras();
JSONObject jsonData1;
jsonData1 = new JSONObject(intent.getExtras().getString( "com.parse.Data" ));
while (itr.hasNext()) {
String key = (String) itr.next();
Log.d("NotificationActivity Logs", "..." + key + " => " + jsonData1.getString(key));
}
In the output (Logcat) I am only getting two keys. alert and push_hash, but not giving any other key which I set like title and description.
Related
I have an app in which server sends some push notification using GCM server, The implementation is done on both side but i am facing problem is that i can't get notification from message body, it is always showing me "null". But i can see message in messag body using debug.
String message = bundle.getString("message");
Log.e(TAG, "onMessageReceived::" + message);
Log.e(TAG, "from::" + from);
and message body is :-
Bundle[{google.sent_time=1497866966996, google.message_id=0:1497866967011288%466cbbd8466cbbd8, notification=Bundle[{priority=high, body=Your wallet has been credited with 1.000 point(s)., title=Wallet credited}], collapse_key=com.s.baty}]
Try ,
Bundle notificationData = bundle.getBundle("notification");
String body = notificationData.getString("body");
It received correct data only
String bodymessage = bundle.getString("body");
Problem is print message after converting Sting
Log.e(TAG, "onMessageReceived::" + message.toString());
You can receive message in onMessage() function of GCMIntentService implementing class. I have created a function for getting proper message and key.
#Override
protected void onMessage(Context context, Intent intent) {
dumpIntent(intent);
}
public void dumpIntent(Intent i) {
Bundle bundle = i.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
Log.e("Intent Data", "[" + key + "=" + bundle.get(key) + "]");
}
}
}
After that you will easily set message with NotificationManager.
I have to show push notification while user is logged in.So when app opens am taking deviceToken.So it will register to server always.But when user logout i dont want to show push notification.So how can i do it?
Now i have done with checking preference value like the below code,
public void onMessageReceived(String from, Bundle data) {
loginPref = getSharedPreferences("loginPreference", Context.MODE_PRIVATE);
mEditPrefs = loginPref.edit();
userID = loginPref.getString("userId", null);
if(userID!=null){
Bundle bundle = data.getBundle("notification");
Log.d("Bundle123::", bundle + "");
if (bundle != null) {
String text = bundle.getString("text");
String body = bundle.getString("body");
String title = bundle.getString("title");
Log.d("text123::", text + "");
Log.d("title123::", title + "");
Intent in = new Intent();
in.setAction("GCM_RECEIVED");
sendBroadcast(in);
sendNotification(title,body);
}
}
But when app backgrounds this condition doesn't work.How can i do it?Because i cant delete the token because when am trying login i have to send device token too..
While logout time tell to back end side to do device token as null.
In Quickblox GCM, when i try to send a message via push to a certain user id, i receive the notification on the sender's device too. I am not sure why, is it the norm in development mode?
QBEvent event = new QBEvent();
event.setUserId(sendToUserId);
event.setType(QBEventType.ONE_SHOT);
event.setEnvironment(QBEnvironment.DEVELOPMENT);
event.setNotificationType(QBNotificationType.PUSH);
event.setPushType(QBPushType.GCM);
HashMap<String, String> data = new HashMap<String, String>();
data.put("data.message", "Message from John");
data.put("data.type", "Notify");
data.put("ParentId",dialogId.toString());
event.setMessage(data);
QBMessages.createEvent(event, new QBEntityCallbackImpl<QBEvent>() {
#Override
public void onSuccess(QBEvent qbEvent, Bundle args) {
System.out.println("GCM Message Sent inside event " );
}
#Override
public void onError(List<String> errors) {
System.out.println("GCM Message ERROR inside event ");
}
});
The above code does send a GCM, but to both devices. The sendToUserId as well as the sender's device.
Am i doing anything wrong?
Actually you don't need to set the user_id field - it comes in a response from server and contains the ID of a sender
Look at Push code sample http://quickblox.com/developers/SimpleSample-messages_users-android#Send_Push_Notifications_from_Application
// recipients
StringifyArrayList<Integer> userIds = new StringifyArrayList<Integer>();
userIds.add(53779);
userIds.add(960);
QBEvent event = new QBEvent();
event.setUserIds(userIds);
So, it should be userIds, not userId
I am trying to build Whatsapp Notification filtering app, where I monitor all notification from Whatsapp and remove messages as per filtering policy.
I can fetch message content using below link code
Extract notification text from parcelable, contentView or contentIntent for first message only
but the problem is I can fetch only first message, if user does not read first message then second message onwards I get only "2 messages from sender" instead of actual message.
NOTE: I am getting
android.text = actual message for first message but its null from second message/notification onwards
android.title = sender
android.summaryText = "n new messages"
any help would be appreciated.
Yes, finally after few hours of googling I design a code which does work for me.
Bundle extras = sbn.getNotification().extras;
CharSequence[] lines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
JSONArray s = new JSONArray();
for (CharSequence msg : lines) {
msg = removeSpaces(msg);
if (!TextUtils.isEmpty(msg)) {
s.put(msg.toString());
}
}
private static String removeSpaces(#Nullable CharSequence cs) {
if (cs == null)
return null;
String string = cs instanceof String ? (String) cs : cs.toString();
return string.replaceAll("(\\s+$|^\\s+)", "").replaceAll("\n+", "\n");
}
here JSONArray s contains all messages that I want
My android app getting same message like "you got message". Even though i'm changing data in server side.
Server-Side code(dot net):
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
+ Label1.Text + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + regId + "";
have to make any changes in GCM app code?
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = getString(R.string.gcm_message);
//String message = intent.getExtra("message");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
On seraching google i found something that we have to replace this
String message = getString(R.string.gcm_message);
with
String message = intent.getExtra("message");
but i am getting error like this "The method getExtra(String) is undefined for the type Intent".Please guide me that what i'm missing in this?
use
String message = intent.getStringExtra("message");
instead of
String message = intent.getExtra("message");
for Getting String Message from Intent
EDIT :
if you are receive data in Bundle instance then change your code as because getExtra is not a method in Intent class :
Bundle bundle = intent.getExtras("bundle_Name_here");
now retrieve all value from bundle using key
Add this to your HTTP POST:
data.notId="2"
For each different notId it will be threated as a different message. I had the same problem for a long time, now I would like to group those messages.