简体   繁体   中英

Unity3D Push Notifications iOS & Android Vuforia & UrbanAirship

I have am working on a game that uses the Vuforia SDK and I want to integrate Push notifications preferably using UrbanAirship (that is what my client requested)

I have been trying to get Push Notifications to work on both iOS AND on android but have had no luck, not with UrbanAirship and not any other way, I have also tried the Pushwoosh Unity plugins.

Problem 1: Is it at all possible to integrate UrbanAirship with Unity3d? Any links/suggestions/samples would be great, I have searched but not been able to find anything relevant.

Problem 2: (if I understand correctly) is that Vurforia SDK requires the MAIN activity in the Android Manifest file, so that prevents me from implementing plugins such as Pushwoosh or other similar plugins, as to use the GCM (Google Cloud Messaging) they need the MAIN activity as well.

Problem 3: Again if I have understood correctly from all the sites/forums/posts I have read, GCM is the way to do push notifications to Android. Reading up a bit about GCM it is a general two messaging API, can it be used for push notifications when the app is not running? or am I completely on the wrong track?

I know, and apologise, for the long question, but this is the first time of tackling Push Notifications, let alone something that will work both on iOS and Android. I would really appreciate any suggestions as how to get this working. TIA!

This is the code from the android plugin I built for Urban Airship

Please note that I extended the application. You also need an intent receiver class.

package com.yourapp.urbanairship;

import android.app.Application;
import android.util.Log;

import com.urbanairship.AirshipConfigOptions;
import com.urbanairship.Logger;
import com.urbanairship.UAirship;
import com.urbanairship.push.CustomPushNotificationBuilder;
import com.urbanairship.push.PushManager;

public class UrbanAirShipPlugin extends Application {


    @Override
    public void onCreate() {

        super.onCreate();

        AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);

    options.productionAppKey ="XXXXXXXXXXXXXXXXXXXXXXXXXX";  // from urban airship
    options.productionAppSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXX"; // from urban airship
    options.developmentAppKey ="XXXXXXXXXXXXXXXXXXXXXXXXXX"; // from urban airship
    options.developmentAppSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXX";// from urban airship
    options.gcmSender = "XXXXXXXXXXXXXX"; // from GOOGLE -> basically your project id

    options.inProduction = true;
    options.analyticsEnabled =false;
    options.minSdkVersion=18;

    //determines which app key to use
    // Take off initializes the services
    UAirship.takeOff(this, options);
    PushManager.shared().setIntentReceiver(IntentReceiver.class);

    }
}

Here is the intent receiver class:

package your.package.name;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.urbanairship.UAirship;
import com.urbanairship.push.GCMMessageHandler;
import com.urbanairship.push.PushManager;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class IntentReceiver extends BroadcastReceiver {

    private static final String logTag = "PushSample";

    public static String APID_UPDATED_ACTION_SUFFIX = ".apid.updated";

    @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.i(logTag, "Received push notification. Alert: "
                + intent.getStringExtra(PushManager.EXTRA_ALERT)
                + " [NotificationID="+id+"]");

            logPushExtras(intent);

        } else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {

            Log.i(logTag, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT));

            logPushExtras(intent);

            Intent launch = new Intent(Intent.ACTION_MAIN);
            launch.setClassName(UAirship.shared().getApplicationContext(), "your.main.activity" );
            launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            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));

            // Notify any app-specific listeners
            Intent launch = new Intent(UAirship.getPackageName() + APID_UPDATED_ACTION_SUFFIX);
            UAirship.shared().getApplicationContext().sendBroadcast(launch);

        } else if (action.equals(GCMMessageHandler.ACTION_GCM_DELETED_MESSAGES)) {
            Log.i(logTag, "The GCM service deleted "+intent.getStringExtra(GCMMessageHandler.EXTRA_GCM_TOTAL_DELETED)+" messages.");
    }

}

/**
 * Log the values sent in the payload's "extra" dictionary.
 * 
 * @param intent A PushManager.ACTION_NOTIFICATION_OPENED or ACTION_PUSH_RECEIVED intent.
 */
    private void logPushExtras(Intent intent) {
        Set<String> keys = intent.getExtras().keySet();
        for (String key : keys) {

            //ignore standard C2DM extra keys
            List<String> ignoredKeys = (List<String>)Arrays.asList(
                "collapse_key",//c2dm collapse key
                "from",//c2dm sender
                PushManager.EXTRA_NOTIFICATION_ID,//int id of generated notification (ACTION_PUSH_RECEIVED only)
                PushManager.EXTRA_PUSH_ID,//internal UA push id
                PushManager.EXTRA_ALERT);//ignore alert
            if (ignoredKeys.contains(key)) {
                continue;
            }
            Log.i(logTag, "Push Notification Extra: ["+key+" : " + intent.getStringExtra(key) + "]");
        }
    }
}

please ALSO note you need to add your plugin com.yourapp.urbanairship name for the application extension to the manifest (next to all the other stuff you need in there):

<application android:allowClearUserData="true" android:icon="@drawable/app_icon" android:label="@string/app_name" android:name="com.yourapp.urbanairship"  android:debuggable="true">

And dont forget whatever else urbanairship needs in your manifest (see also here )

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