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.
Related
In order to read android notifications we need to create a custom class that extends NotificationListenerService class. But I am not able to understand what the inside this method does?
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG,"********** onNotificationPosted");
Log.i(TAG,"ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText + "t" + sbn.getPackageName());
Intent i = new Intent("com.example.readandroidnotification.NOTIFICATION_LISTENER_EXAMPLE");
i.putExtra("notification_event","onNotificationPosted :" + sbn.getPackageName() + "n");
sendBroadcast(i);
}
Can anyone explain the working of this code?
It logs to the system log that a notification has been posted.
It logs to the system log the ID of the notification, the summary text of the notification, and the package name of the app that created it.
It then creates a new Intent to broadcast some info about the notification that was posted.
It puts the package name of the app that posted the notification as an extra to the Intent.
It sends that Intent as a broadcast, allowing another component of the app to use that info.
I recommend reading some documentation about Android:
Broadcast overview
StatusBarNotification
I am new to android programming.I want my app to display a pop-up notification( its probably called a heads up notification) on the top of the screen whether it is in background or foreground and add sound to it but cant figure out how. The code is very basic.
MyFirebaseMessageService.java
public class MyFirebaseMessageService extends FirebaseMessagingService {
public static final String TAG = "FCM";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message: " + remoteMessage.getNotification().getBody());
}
}
MyFirebaseIdService.java
public class MyFirebaseIDService extends FirebaseInstanceIdService {
private static String TAG = "INSTANCEID";
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed Token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
}
}
Do you want your notification to be on the screen, or on the notification bar?
For a simple on screen notification use a Toast.
Toast.makeText(getApplicationContext(),"Your message",Toast.LENGTH_SHORT).show();
For a notification bar notification take a look at NotificationBuilder.
You can make your notification make sound as well.
I am trying to use FCM to send UpStream Message, so I followed the tutorial on google and it works.
As shown in the code below in MainActivity, I send Upstream message when the button is clicked, then in MyAndroidFirebaseMsgService I should see a Log message as shown
below in MyAndroidFirebaseMsgService.
But what happen is, the Log messages in MyAndroidFirebaseMsgService in onMessageSent in do not get displayed even I kept pressing the button several times.
the Log message in MyAndroidFirebaseMsgService in onMessageSent can be displayed only if sent a downstream messagefrom FCM to the App, in this case, both the Logs in
in MyAndroidFirebaseMsgService will be displayed.
Please let me know why the Log message in onMessageSent is not getting displayed once there is an UpStream message sent?and how to fix it.
Mainactivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnSendUpstreamMsg = (Button) findViewById(R.id.btn_send_upstream_message);
mBtnSendUpstreamMsg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseMessaging fm = FirebaseMessaging.getInstance();
fm.send(new RemoteMessage.Builder("673xxxxx" + "#gcm.googleapis.com")
.setMessageId("2")
.addData("my_message", "Hello World")
.addData("my_action","SAY_HELLO")
.build());
}
});
}
MyAndroidFirebaseMsgService:
public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {
private final static String TAG = MyAndroidFirebaseMsgService.class.getSimpleName();
#Override
public void onMessageSent(String s) {
super.onMessageSent(s);
Log.d(TAG, "onMessageSent: upstream message");
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "onMessageReceived: downstream message");
//Log data to Log Cat
Log.d(TAG, "onMessageReceived->From: " + remoteMessage.getFrom());
Log.d(TAG, "onMessageReceived->Notification Message Body: " + remoteMessage.getNotification().getBody());
//create notification
createNotification(remoteMessage.getNotification().getBody());
}
private void createNotification( String messageBody) {
Intent intent = new Intent( this , ResultActivity.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Android Tutorial Point FCM Tutorial")
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
}
Yes, is possible to send a Firebase messaging push notification and receive it in all app life cycles using onMessageReceived.
But is necessary to change the default Firebase behaviour, intercepting the intent request before everything else.
** IMPORTANT NOTE **
This was a pretty stupid idea from Firebase by remove the developers processment capability when the FCM message arives with the notification message format, but not for data message.
This created a bunch of "workarounds" in many solutions, which made the analythics and everything else being messed up.
If I had designed this solution, I would always call the onMessageReceived method with a completion handle. Let the developer decide what to do (free tip for you, Firebase).
Use onMessageReceived is the correct way to do. This method is the only one who brings RemoteMessage object, that have every information what you need. It was designed for it. You are on correct path.
** HOW TO DO **
In your Firebase Class MyAndroidFirebaseMsgService, which extends FirebaseMessagingService, override the public method handleIntent to intercep the intent request before Firebase catch it.
#Override
public void handleIntent(Intent intent){
if(intent.hasExtra("google.message_id")){
intent = handleFirebaseIntent(intent);
}
super.handleIntent(intent);
}
After, transform the notification message package into an data message, removing all "gcm.notification.%" and "gcm.n.%" extras from intent, and translating "gcm.notification.title", "gcm.notification.body" and "gcm.notification.image" elements into what you need:
// Thank you Google, for that brilliant idea to treat notification message and notification data
// differently on Android, depending of what app life cycle is. Because of that, all the developers
// are doing "workarounds", using data to send push notifications, and that's not what you planned for.
// Let the developers decide what to do on their apps and ALWAYS deliver the notification
// to "onMessageReceived" method. Its simple, is freedom and its what the creative ones need.
private Intent handleFirebaseIntent(Intent intent){
//printIntentExtras(intent);
String FCM_TITLE_KEY = "gcm.notification.title";
String FCM_BODY_KEY = "gcm.notification.body";
String FCM_IMAGE_KEY = "gcm.notification.image";
String title = intent.getStringExtra(FCM_TITLE_KEY);
String body = intent.getStringExtra(FCM_BODY_KEY);
String image = intent.getStringExtra(FCM_IMAGE_KEY);
// Remove the key extras that identifies an Notification type message
Bundle bundle = intent.getExtras();
if (bundle != null) {
for (String key : bundle.keySet()) {
if (key.startsWith("gcm.notification.") || key.startsWith("gcm.n."))
{
intent.removeExtra(key);
}
}
}
Boolean isTitleEmpty = StringUtils.isNullOrEmpty(title);
Boolean isBodyEmpty = StringUtils.isNullOrEmpty(body);
Boolean isImageEmpty = StringUtils.isNullOrEmpty(image);
// Notification title and body has prevalence over Data title and body
if(
!isTitleEmpty || !isBodyEmpty || !isImageEmpty
){
// This is my personalized translation method, designed for my solution.
// Probably you gonna need to do it by your own
String contentData = intent.getStringExtra(Definitions.PUSH_NOTIFICATION_CONTENT);
Map<String, Object> content;
if(StringUtils.isNullOrEmpty(contentData)){
content = new HashMap<String, Object>();
content.put(Definitions.NOTIFICATION_ID, new Random().nextInt(65536) - 32768);
content.put(Definitions.NOTIFICATION_CHANNEL_KEY, "basic_channel" );
} else {
content = JsonUtils.fromJson(new TypeToken<Map<String, Object>>(){}.getType(),contentData);
}
if(!isTitleEmpty) content.put(Definitions.NOTIFICATION_TITLE, title);
if(!isBodyEmpty) content.put(Definitions.NOTIFICATION_BODY, body);
if(!isImageEmpty){
content.put(Definitions.NOTIFICATION_BIG_PICTURE, image);
content.put(Definitions.NOTIFICATION_LAYOUT, NotificationLayout.BigPicture.toString());
}
contentData = JsonUtils.toJson(content);
intent.putExtra(Definitions.PUSH_NOTIFICATION_CONTENT, contentData);
}
//printIntentExtras(intent);
return intent;
}
private void printIntentExtras(Intent intent){
Bundle bundle;
if ((bundle = intent.getExtras()) != null) {
for (String key : bundle.keySet()) {
System.out.println(key + " : " + (bundle.get(key) != null ? bundle.get(key) : "NULL"));
}
}
}
You can check my entire solution here.
Does anyone know how can I check if there are any notifications programmatically in android?
I want to check if there is any notification currently available in the notifications list.
for example, if there is any notification, the LED light is turned on and if the notifications list is cleared, the LED light is turned off.
I just want to know the condition or the code that allow us to check if there are any notifications available in the notifications list.
if ( condition - there are any notifications )
// my code
You can use NotificationListener API, which is available on Android 4.3+. To do that you just simply need to create a simple Service that extends NotificationListenerService.
Here is some sample code
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
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, "Notification posted");
Log.i(TAG, "ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText + "t" + sbn.getPackageName());
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG, "Notification Removed");
Log.i(TAG, "ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText + "t" + sbn.getPackageName());
}
}
A complete tutorial is available here
Prior to this version of Android, you can make a hack through the AccessibilityService as described here
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);