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.
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.
Is there any way that I could directly redirect the log message in Android to a string?
For example, I have a log message in Android.
Log.v(TAG, "First Name" + firstName);
Log.v(TAG, "Last Name" + lastName);
I have a string
String nameMessage = "Please check your credetials\n" ;
I want to concatenate the log to the message.
One way is to concatenate manually.
I am looking If we could redirect the log to message.
I'm not sure if I am understanding your question right, but technically you could just create a function that appends the log message and then actually logs it?
Example:
private StringBuilder errorMessage;
public void log(String tag, String message) {
errorMessage.append(message).append(System.getProperty("line.separator"));
Log.v(tag, message);
}
public void getErrorMessage() {
return errorMessage.toString();
}
And then, you'd just replace all your calls to Log#v with a call to #log(tag, message).
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
So I am sending on the server side like so, using node-gcm
(https://github.com/ToothlessGear/node-gcm):
var gcm = require('node-gcm');
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: true,
timeToLive: 3,
data: {
type: 'pong',
message: 'Hello Android!'
}
});
var sender = new gcm.Sender(myAPIKey);
var registrationIds = [aDoc.registrationId];
sender.send(message, registrationIds, 4, function (err, result) {
console.log(result);
});
And then on the client side, in my BroadcastReceiver, I get the Logcat message printing and the receive notification, but the extras (from the intent.getStringExtra("data") is null. How do I get it properly? I cannot find how to do it anywhere. The registration case works perfectly.
#Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
Log.i(TAG, "GCM Received");
switch(action){
case GCM.ACTION_REGISTRATION:
String registrationId = intent.getStringExtra(GCM.KEY_REG_ID);
Log.i(TAG, "action received: registration: " + registrationId);
...
break;
case GCM.ACTION_RECEIVE:
String extras = intent.getStringExtra("data");
Log.v(TAG, "" + extras);
...
...
extras is consistantly null
Through experimentation, I have solved my problem. Instead of accesssing the data object directly, node-gcm adds the key-value pairs to the intent's extras directly:
Wrong:
// returns null
String dataJson = intent.getStringExtra("data");
Right:
// returns "pong"
String type = intent.getStringExtra("type");
I recently changed my c2dm push aaplucation to GCM. A new problem appeared. Its now receiving '?' instead of 'ö','ï', ...
My server logs correct Strings, but the application receives '?' insteads.
Do you think it could have something to do with GCM?
My code is the following:
public static void displayMessage(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String message = (String) extras.get("message");
Log.v("extras", extras.toString());
Util.generateNotification(context, message, intent);
}
}
and the log is then:
10-02 22:18:23.671: V/intent(29809): Bundle[{message={"name":"j?rg"},
message_id=8bb60eee-3a93-4075-b606-40495511a4da, collapse_key=do_not_collapse, from=160085429222}]
Best regards!
I dont think so there is problem in GCM. Try to use UTFEncoding for your message.
String output = new String(name.getBytes("8859_1"), "utf-8");