IOS use NotificationCenter likes below:
let failureObserver = NSNotificationCenter.defaultCenter().addObserverForName(downloadEndFailureNotificationName, object: nil, queue: nil) { (notification) in
//Process failed result
self._processFailureResultData(forID: connectionID)
}
So, what is using instead of NotificationCenter in Android?
You use Notification.Builder to build Notification instance. If you are looking for Observer similar to ios, look into Broadcast Receiver
LocalBroadcastManager
is a very heavyweight solution which reflects a misunderstanding of what NSNotificationCenter
does.
The best tool for the job is very likely something like Guava's EventBus . Here's a simple way to use it.
[NSNotificationCenter defaultCenter]
equivalent. In the Guava library and the description below, Apple's notifications become events , and an observer becomes a consumer . An object that posts an event is called a producer . EventBus eventBus = new EventBus();
public class MyCustomEvent {
public MyCustomEvent() {}
}
MyCustomEvent
. You need to use the event bus instance we created in step 1. The process of registration involves registering an instance of a class with the event bus. This class needs to have one or more methods annotated with the @Subscribe
annotation. Such a method must take as the only argument the event class you defined above. The name of the method doesn't matter. public class MyConsumerClass {
public MyConsumerClass(EventBus eventBus) {
eventBus.register(new Object() {
@Subscribe
public void receivedCustomEvent(MyCustomEvent event) {
// Do something with the received notification/event.
}
});
}
...
}
eventBus.post(new MyCustomEvent());
For more information read up on the Guava library.
Like NotificationCenter you can register and unregister object as observers. You can send broadcast messages to simulate iOS notifications behaviour.
For the usage refer to this StackOverflow question
Create a Notification.Builder :
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Then issue the notification:
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Add permissions into AndroidManifest.xml
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE"/>
public class GCMBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = GCMBroadcastReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if ("com.google.android.c2dm.intent.REGISTRATION".equals(intent.getAction())) {
// Register Localytics (this will call Localytics.handleRegistration(intent))
new PushReceiver().onReceive(context, intent);
} else if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) {
// DO SOMETHING
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.