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.
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 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
I'm able to get data from each single activity but unable to get when calling from both activities at a time.
I have 3 activities
1.GoogleLogin
2.FacebookLogin
3.MainActivity
I have this in my GoogleLogin:
#Override
public void onConnected(Bundle connectionHint) {
// Reaching onConnected means we consider the user signed in.
Intent i = new Intent(getApplicationContext(),MainActivity.class);
Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("Google", "Logged in using Google Account");
bundle.putString("GoogleUsername", currentUser.getDisplayName());
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
// Indicate that the sign in process is complete.
mSignInProgress = STATE_DEFAULT;
}
I have this in my FacebookLogin:
if (session.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Intent i = new Intent(FBMainActivity.this,MainActivity.class);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("Facebook", "Logged in using Facebook Account");
bundle.putString("LastName", user.getLastName());
bundle.putString("FirstName", user.getFirstName());
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
}
}
}).executeAsync();
}
In My MainActivity I get them as shown:
// 1. get passed intent
Intent facebookintent = getIntent();
// 2. get message value from intent
String lastName = facebookintent.getStringExtra("LastName");
String firstName = facebookintent.getStringExtra("FirstName");
// 3. get bundle from intent
Bundle facebookbundle = facebookintent.getExtras();
// 4. get status value from bundle
String facebookstatus = facebookbundle.getString("Facebook");
// 1. get passed intent
Intent googleintent = getIntent();
// 2. get message value from intent
String userName = googleintent.getStringExtra("GoogleUsername");
// 3. get bundle from intent
Bundle googlebundle = facebookintent.getExtras();
// 4. get status value from bundle
String googlestatus = googlebundle.getString("Google");
if (facebookstatus=="Logged in using Facebook Account"){
// 3. show message on textView
((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + lastName + " " + firstName);
}else if (googlestatus=="Logged in using Google Account"){
// 3. show message on textView
((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + userName);
}
I'm unable to get GoogleUsername but able to get them individually when able to call from each single activity.
As per the logic flow, your activity can get started by either facebook or google login.
so you have to check and use it appropiately. do something like this
// 1. get passed intent
Intent intent = getIntent();
if (intent.getStringExtra("Facebook") != null){
// 2. get message value from intent
String lastName = intent.getStringExtra("LastName");
String firstName = intent.getStringExtra("FirstName");
if(intent.getStringExtra("Facebook").equals("Logged in using Facebook Account")){
((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + lastName + " " + firstName);
}
}else if(intent.getStringExtra("Google") != null){
// 2. get message value from intent
String userName = googleintent.getStringExtra("GoogleUsername");
// 3. get bundle from intent
Bundle googlebundle = facebookintent.getExtras();
if(intent.getStringExtra("Google").equals("Logged in using Google Account")){
((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + userName);
}
}
you need check before check googleStatus and facebookStatus. ( in java we dont compare two String with == , compare that with .equals() method )
you need check that in your bundle google account is exists or facebook account, for that you need following code:
if (bundle != null)
{
if ( bundle.containsKey("Facebook") )
{
// user logged in with facebook account
}
else if (bundle.containsKey("Google") )
{
// check google account
}
}
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.
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.