Android, Urban AirShip - change the message - android

How to change the notification message in Android to localised it?
I just want to change the message what I get from Urban AirShip before it will be display on notification bar?

You can using this method: PushManager.shared().setNotificationBuilder(null); and send your own notification with android notification from the SDK

Yes you can modify the message once you receive the message from Urban AirShip for your internal use in the app but can't show the modified message in the notification bar.
You can check the message in your IntentReceiver
public class IntentReceiver extends BroadcastReceiver {
private static final String logTag = "Hey";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(logTag, "Received intent: " + intent.toString());
String action = intent.getAction();
if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);
Log.v(logTag, "Received push notification. Alert: " + intent.getStringExtra(PushManager.EXTRA_ALERT)
+ ". Payload: " + intent.getStringExtra(PushManager.EXTRA_STRING_EXTRA) + ". NotificationID="+id);
//can get your message here
} else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
Log.v(logTag, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT)+ ".Payload:" + intent.getStringExtra("PushManager.EXTRA_STRING_EXTRA"));
Intent launch = new Intent(Intent.ACTION_MAIN);
UAirship.shared().getApplicationContext().startActivity(launch);
} else if (action.equals(PushManager.ACTION_REGISTRATION_FINISHED)) {
Log.i(logTag, "Registration complete. APID:" + intent.getStringExtra(PushManager.EXTRA_APID)
+ ". Valid: " + intent.getBooleanExtra(PushManager.EXTRA_REGISTRATION_VALID, false));
}
}
}

I found the solution, its kind a dirty one, but right know I need to use this.
Just after get notification from Urban AirShip I cancel all and send my own with changed message.

I read the answers and came up with a combined solution.
You want to disable UrbanAirship's default notification handler. If you do that, it won't generate and show you the notifications at all.
PushManager.shared().setNotificationBuilder(null); (Using David T's suggestion)
You want to build your own notification. This can be done inside your IntentReceiver. This link will do the trick.
Hope this helps others.

Please using that code to disable Urban Airship's notifications, override BasicPushNotificationBuilder:
BasicPushNotificationBuilder nb = new BasicPushNotificationBuilder() {
#Override
public Notification buildNotification(String alert,
Map<String, String> extras) {
return null;
}
};
// Disable notifications
PushManager.shared().setNotificationBuilder(nb);

Related

android firebase reg id recived but notification not show in phone display

I will try to show personal single notification on my phone tray, but I can't rich, so help.
I am having an issue with FireBase Cloud Messaging in which I get the Token from the device and send the notification test through the Google Firebase notification console, however, the notification is never logged nor pushed to the android virtual device. The documentation for FCM is almost exactly the code that I have below and little else in the way of what else you would have to do to get push notifications working with firebase. I have gone through all of the setup information (build.gradle additions, Installing google play services, etc...) as specified in the documentation, but still do not have messages generating. What is wrong with the code that I am not receiving my push notifications to the logcat or the device? Please let me know any further information that would be helpful. Thanks.
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION))
{
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
txtMessage.setText(message);
}
}
};
displayFirebaseRegId();
}
private void displayFirebaseRegId() {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
String regId = pref.getString("regId", null);
Log.e(TAG, "Firebase reg id: " + regId);
if (!TextUtils.isEmpty(regId))
txtRegId.setText("Firebase Reg Id: " + regId);
else
txtRegId.setText("Firebase Reg Id is not received yet!");
}
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.REGISTRATION_COMPLETE));
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.PUSH_NOTIFICATION));
NotificationUtils.clearNotifications(getApplicationContext());
}
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
And I will add lib of fire base messaging is:
compile 'com.google.firebase:firebase-messaging:11.0.4'
You don't need to subscribe inside the BroadcastReceiver you can just do it inside the onTokenRefresh method in the FirebaseInstanceIdService
You don't need to get the push notification in the BroadcastReceiver, you have to do it inside the onMessageReceive in the FirebaseMessagingService
FCM is extremely unreliable with emulators, simply use a real device, I have struggled with this and in some cases I even get the notification days later when opening the emulator for other projects, test this with real phones

How to parse the stacked messages in a notification (Whatsapp notification)

I am trying to parse the Whatsapp notifications that i receive on my device using NotificationListenerService.
On receiving a notification for the first time, i am able to retrieve the Text message. However, when the second message arrives, i get the message: "2 new messages". So basically here the messages in the notification are getting stacked.
Below is the code i have used:
public class NLService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG,"********** onNotificationPosted");
String key = sbn.getKey();
sbn.getNotification().tickerText + "\n Package Name:" + sbn.getPackageName());
Log.i(TAG,"getTAG:"+sbn.getTag());
Log.i(TAG,"getKey:"+sbn.getKey());
Log.i(TAG,"getGrpKey:"+sbn.getGroupKey());*/
Bundle bundle = sbn.getNotification().extras;
String title = bundle.getString("android.title");
String text = bundle.getCharSequence("android.text").toString();//This gives me the actual message, the first time the notification arrives
Log.i(TAG,"Bundle is:"+bundle);
Log.i(TAG,"Title:"+title);
Log.i(TAG,"Text:"+text);
NLService.this.cancelNotification(key);
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG,"********** onNOtificationRemoved");
Log.i(TAG,"ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText +"\t" + sbn.getPackageName());
}
}
So my query here is, how do i parse/fetch messages from this notification? Or is there another way to fetch incoming whatsapp messages?
One approach that i can think of is marking the message as "Read", but has anyone able to mark a whatsapp message as "read" programmatically?
I have read most of the older posts on Stackoverflow and also whatsapp official site provides us a way of send a message.
Any sort of suggestions shall be really helpful.

Open android app on clicking on the push notification with urban airship?

I am sending the push notifications with the help of Urban Airship, and getting the notifications successfully also. But when I am clicking on the notification, it is not opening my app. So what can I do for opening my app ?
and getting the following error in logcate:-
02-10 18:53:44.137: W/xxx - UALib(6840): Activity com.aaa.yyy.SplashActivity#40dcb458 was not manually added during onStart(). Call UAirship.shared().getAnalytics().activityStarted in every activity's onStart() method.
Yes after doing the Google I got the answer:-
We need to create an IntentReceiver.java class as follows:-
public class IntentReceiver extends BroadcastReceiver{
private static final String logTag = "PushSample";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(logTag, "Received intent: " + intent.toString());
String action = intent.getAction();
if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);
Log.i(logTag, "Received push notification. Alert: "
+ intent.getStringExtra(PushManager.EXTRA_ALERT)
+ " [NotificationID="+id+"]");
logPushExtras(intent);
}else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
Log.i(logTag, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT));
logPushExtras(intent);
Intent launch = new Intent(Intent.ACTION_MAIN);
launch.setClass(UAirship.shared().getApplicationContext(), MainActivity.class);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
UAirship.shared().getApplicationContext().startActivity(launch);
}else if (action.equals(PushManager.ACTION_REGISTRATION_FINISHED)) {
Log.i(logTag, "Registration complete. APID:" + intent.getStringExtra(PushManager.EXTRA_APID)
+ ". Valid: " + intent.getBooleanExtra(PushManager.EXTRA_REGISTRATION_VALID, false));
}
}
private void logPushExtras(Intent intent) {
Set<String> keys = intent .getExtras().keySet();
for(String key: keys){
List<String> ignoredKeys = (List<String>)Arrays.asList("collapse_key", "from", PushManager.EXTRA_NOTIFICATION_ID,PushManager.EXTRA_PUSH_ID, PushManager.EXTRA_ALERT);
if(ignoredKeys.contains(key)){
continue;
}
Log.i(logTag, "Push Notification Extra: ["+key+" : " + intent.getStringExtra(key) + "]");
} }
}
After that we need to call the following method in PushNotification.java.
Here is the code.
public class PushNotification extends Application{
#Override
public void onCreate() {
AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
options.developmentAppKey = "xxx";
options.developmentAppSecret = "yyy";
options.productionAppKey = "zzz";
options.inProduction= false;
UAirship.takeOff(this, options);
PushManager.enablePush();
String apid = PushManager.shared().getAPID();
Logger.info("My Application onCreate - App APID: " + apid);
PushManager.shared().setIntentReceiver(IntentReceiver.class);
}
}
In response to your logcat error:
UALib(6840): Activity com.aaa.yyy.SplashActivity#40dcb458 was not manually added during onStart(). Call UAirship.shared().getAnalytics().activityStarted in every activity's onStart() method.
By default, Urban Airship turns on analyticsEnabled in the AirshipConfigOptions (see documentation).
Prior to Android API 14, you had to manually call activityStarted() in every activity's onStart() method (as the logcat warning suggests or see documentation). If your app’s minSDKVersion >= 14 (Ice Cream Sandwich), you no longer need to modify any of your activities. Make sure to set the minSDKVersion in the airshipconfig.properties in order to prevent any missing instrumented analytic warnings.
If you would like to manually instrument your class, update your Activity’s onStart and onStop methods with the following:
#Override
public void onStart() {
super.onStart();
Analytics.activityStarted(this);
}
#Override
public void onStop() {
super.onStop();
Analytics.activityStopped(this);
}
Note:
According to the UA v4 to v5 migration guide the methods to report activity started and stopped are now static (which is the sample code above). If you need to support an older version of UA, then you'd use the sample code from logcat:
UAirship.shared().getAnalytics().activityStarted(this);

DevicePolicyManager, locknow when gcm message received

I am trying to remotely lock my phone when a gcm PUSH notification with a keyword arrives.
Inside the GcmIntentService class, within the onHandleIntent I have included
message = extras.getString("message");
if(message.equals("LOCK")){
gcmaction(message);
}
else{
sendNotification("Message: " + extras.getString("message"));
Log.i(TAG, "Message: " + extras.toString());
}
So if a message arrives saying LOCK, it should go to the gcmaction method. Otherwise go to the notification method.
private void gcmaction(String string) {
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
//lock action
mDPM.resetPassword(Password,
DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
mDPM.lockNow();
}
sendNotification is correctly showing notfications but nothing happens when I send "LOCK" ... can someone tell me what I am missing ?
Thanks

Notification received but not showing

I'm adding Urban Airship push notifications to my Android app. My custom BroadcastReceiver is receiving the notifications when I send a test push. Here's the onReceive() method.
public class IntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent == null)
return;
String action = intent.getAction();
if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0); //Breakpoint here
Log.w("my app", "Received push notification. Alert: "
+ intent.getStringExtra(PushManager.EXTRA_ALERT)
+ " [NotificationID="+id+"]");
logPushExtras(intent);
} else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
//Other stuff here
}
Execution stops at the breakpoint (position shown in the comment) and the details are logged.
No notification is shown in the notification area though. In my other app with UA push notifications, I think this was all I did, but it doesn't seem to be working this time.
I suspect I've done something wrong in my manifest file or that I've forgotten to implement a class somewhere.
Any ideas?
Extra Info
Extended Application:
public class ExtendedApplication extends Application {
public void onCreate(){
AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
UAirship.takeOff(this, options);
PushManager.enablePush();
PushPreferences prefs = PushManager.shared().getPreferences();
Log.e("my app", "My Application onCreate - App APID: " + prefs.getPushId());
PushManager.shared().setIntentReceiver(IntentReceiver.class);
}
}
Edit
I've tried adding
PushManager.shared().setNotificationBuilder(new BasicPushNotificationBuilder());
to my ExtendedApplication as suggested in this post.
I was sending a test push with a blank "Alert" field from the UA site. I tried with some text in there and now it works. I don't know why, but I don't think that's something that should fail silently.
Moral of the story: Don't leave your Alert field blank.

Categories

Resources