简体   繁体   中英

Which object use in Android instead of NotificationCenter in IOS?

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.

  1. Create an EventBus instance. This is the [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();
  1. Create an event type . This is a Java class of your own design. You could have it hold any information you need to pass from the producer to your consumers.
public class MyCustomEvent {
  public MyCustomEvent() {}
}
  1. In your consumer class, register for event notifications on 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.
      }
    });
  }
  ...
}
  1. On the producer side, you can now post notifications (events) that are received by the registered consumers. Make sure you use the same event bus instance defined in step 1, which has your consumers registered with.
eventBus.post(new MyCustomEvent());

For more information read up on the Guava library.

Use LocalBroadcastManager

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

  1. Create a notification

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());
  1. Listen to notification (using GCM permissions)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM