简体   繁体   English

Unity3D推送通知iOS和Android Vuforia和UrbanAirship

[英]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) 我正在开发一个使用Vuforia SDK的游戏,我希望最好使用UrbanAirship集成推送通知(这是我的客户要求的)

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. 我一直在尝试让Push Notifications在iOS和android上都可以运行,但是没有运气,没有UrbanAirship,也没有其他任何方式,我也尝试了Pushwoosh Unity插件。

Problem 1: Is it at all possible to integrate UrbanAirship with Unity3d? 问题1:是否可以将UrbanAirship与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. 问题2 :(如果我理解正确的话)是Vurforia SDK需要在Android Manifest文件中进行MAIN活动,因此阻止我实施诸如Pushwoosh之类的插件或其他类似插件,以使用它们所需的GCM(Google云消息传递)以及主要活动。

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. 问题3:同样,如果我从阅读过的所有站点/论坛/帖子中都能正确理解,GCM就是向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? 阅读有关GCM的一些知识,它是一个通用的两个消息传递API,可以在应用未运行时将其用于推送通知吗? 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. 我知道,很抱歉,这个问题很长,但这是第一次处理推送通知,更不用说在iOS和Android上都可以使用的东西了。 I would really appreciate any suggestions as how to get this working. 对于如何使它正常工作,我将不胜感激。 TIA! TIA!

This is the code from the android plugin I built for Urban Airship 这是我为Urban Airship构建的android插件中的代码

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): 另请注意,您需要将应用程序扩展名的插件com.yourapp.urbanairship名称添加到清单中(在此处需要的所有其他内容旁边):

<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 ) 别忘了清单中还有其他城市飞艇需求(另请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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