I am working on an application on which i want to run a method when a message or notification is received from another device. Notification or method will be send from the sender device and method should run on the Receiver device. Please guide me . Thanks in advance
When the notification is received, You will be notified if you have used Notification Listener Service
@TargetApi(18)
public class NotificationsListenerService extends android.service.notification.NotificationListenerService {
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public void onListenerConnected() {
Log.d("NotificationListener", "listenerconnected");
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
//here check notification and call method
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.d("NotificationListener","removed "+ sbn.getPackageName() + " notification");
}
To use this service , You need Notification Access
String enabledNotificationListeners = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
String packageName = getPackageName();
if (enabledNotificationListeners == null || !enabledNotificationListeners.contains(packageName)) {
startActivityForResult(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"), 13);
Toast.makeText(getApplicationContext(),"Turn on Notification Access for \"App\"",Toast.LENGTH_SHORT).show();
} else {
startService(this, NotificationsListenerService.class);
}