Android Open Source - wonderpush-android-sdk Wonder Push Broadcast Receiver






From Project

Back to project page wonderpush-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project wonderpush-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.wonderpush.sdk;
/*from  ww w  .j a  v a2 s . c  om*/
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;

/**
 * Push notification receiver.
 *
 * <p>
 *   Please look at the <a href="../../../packages.html#installing-sdk--configuring-sdk">package index</a> for more information
 *   on how to setup this receiver.
 * </p>
 *
 * <p>
 *   This {@link BroadcastReceiver} will receive all push notifications and handle them to {@link WonderPush},
 *   like you would, by calling manually {@link WonderPush#onBroadcastReceived(Context, Intent, int, Class)}.
 * </p>
 *
 * <p>
 *   Troubleshooting tip: Make sure you added the {@code "notificationIcon"} and {@code "activityName"}
 *   {@code <meta-data>} tags under the corresponding {@code <receiver>} tag of your {@code <application>},
 *   in your {@code AndroidManifest.xml}.
 * </p>
 */
public class WonderPushBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = WonderPush.TAG;

    private static final String METADATA_ICON_KEYNAME = "notificationIcon";
    private static final String METADATA_ACTIVITY_KEYNAME = "activityName";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            WonderPush.logDebug("Received a push notification!");
            ComponentName receiver = new ComponentName(context, this.getClass());
            try {
                Bundle metaData = context.getPackageManager().getReceiverInfo(receiver, PackageManager.GET_META_DATA).metaData;
                int iconId = metaData.getInt(METADATA_ICON_KEYNAME, -1);
                String activityName = metaData.getString(METADATA_ACTIVITY_KEYNAME);
                if (iconId == -1) {
                    Log.e(TAG, "You should specify a <meta-data android:name=\"notificationIcon\" android:resource=\"@drawable/YourApplicationIcon\"/> in your Receiver definition of your AndroidManifest.xml");
                    return;
                }
                if (activityName == null) {
                    Log.e(TAG, "You should specify a <meta-data android:name=\"activityName\" android:value=\"com.package.YourMainActivity\"/> in your Receiver definition of your AndroidManifest.xml");
                    return;
                }
                WonderPushGcmClient.onBroadcastReceived(context, intent, iconId, Class.forName(activityName).asSubclass(Activity.class));
            } catch (NameNotFoundException e) {
                // If we are called, then at least
                // <receiver android:name="com.wonderpush.sdk.WonderPushBroadcastReceiver"
                //     android:permission="com.google.android.c2dm.permission.SEND" />
                // has been added to the AndroidManifest.xml (or this function has been called manually)
                Log.e(TAG, "Could not get the receiver meta-data", e);
            } catch (ClassNotFoundException e) {
                Log.e(TAG, "The activity provided in <meta-data android:name=\"activityName\" android:value=\"com.package.YourMainActivity\"/> in your Receiver definition of your AndroidManifest.xml doesn't exist", e);
            } catch (ClassCastException e) {
                Log.e(TAG, "The class provided in <meta-data android:name=\"activityName\" android:value=\"com.package.YourMainActivity\"/> in your Receiver definition of your AndroidManifest.xml is not an Activity", e);
            }
        } catch (Exception e) {
            Log.e(TAG, "Unexpected error", e);
        }
    }

    static int getNotificationIcon(Context context) {
        ComponentName receiver = new ComponentName(context, WonderPushBroadcastReceiver.class);
        try {
            Bundle metaData = context.getPackageManager().getReceiverInfo(receiver, PackageManager.GET_META_DATA).metaData;
            return metaData.getInt(METADATA_ICON_KEYNAME, -1);
        } catch (NameNotFoundException e) {
            return -1;
        }
    }

}




Java Source Code List

com.wonderpush.sdk.QueryStringParser.java
com.wonderpush.sdk.WonderPushBroadcastReceiver.java
com.wonderpush.sdk.WonderPushCompatibilityHelper.java
com.wonderpush.sdk.WonderPushConfiguration.java
com.wonderpush.sdk.WonderPushDialogBuilder.java
com.wonderpush.sdk.WonderPushGcmClient.java
com.wonderpush.sdk.WonderPushInitializer.java
com.wonderpush.sdk.WonderPushJobQueue.java
com.wonderpush.sdk.WonderPushOnUpgradeReceiver.java
com.wonderpush.sdk.WonderPushRequestParamsDecorator.java
com.wonderpush.sdk.WonderPushRequestVault.java
com.wonderpush.sdk.WonderPushRestClient.java
com.wonderpush.sdk.WonderPushService.java
com.wonderpush.sdk.WonderPushUriHelper.java
com.wonderpush.sdk.WonderPushView.java
com.wonderpush.sdk.WonderPush.java
com.wonderpush.sdk.package-info.java