【问题标题】:Phonegap Push Notifications for Android适用于 Android 的 Phonegap 推送通知
【发布时间】:2012-08-12 11:23:46
【问题描述】:

我正在使用 Phonegap 和 jQuery Mobile 为 Android 构建一个应用程序。我想实现推送通知,我找到了一些方法,例如:Urban-Air & Phonegap Plugins

但他们似乎不支持 Cordova 1.9... 那么我可以使用其他新版本吗?

【问题讨论】:

标签: jquery-mobile cordova push-notification


【解决方案1】:

您可以尝试从 Pushwoosh 推送 SDK:http://pushwoosh.com,它们是免费的并且已经支持 Cordova 1.9 和 GCM(与 UrbanAirship 不同)。

【讨论】:

    【解决方案2】:

    Urban Airship 如果付费服务和提供的插件仅适用于 iOS...Android 现在使用 CGM 发送 PUSH 通知。

    由于 CGM 是相当新的,并且之前是 C2DM,我手边没有任何 CGM 手册,但也许我的这段代码可以帮助您开始开发:

    主应用 JAR 文件:

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // register for PUSH notifications - you will need a registered Google e-mail for it
        C2DMessaging.register(this /*the application context*/, DeviceRegistrar.SENDER_ID);
        super.loadUrl("file:///android_asset/www/index.html");
    }
    

    DeviceRegistrar.java

    package YOURPACKAGE;
    
    import android.content.Context;
    import android.util.Log;
    
    public class DeviceRegistrar {
        public static final String SENDER_ID = "YOUR-GOOGLE-REGISTERED-EMAIL";
        private static final String TAG = "YOUR_APP_NAME";
        // just so you can work with the registration token from C2DM
        public static String token;
    
        public static void registerWithServer(Context context, String registrationId)
        {
            token = registrationId;
            // insert code to supplement this device registration with your 3rd party server
            Log.d(TAG, "successfully registered, ID = " + registrationId);
        }
    
        public static void unregisterWithServer(Context context, String registrationId)
        {
            // insert code to supplement unregistration with your 3rd party server
            Log.d(TAG, "succesfully unregistered with 3rd party app server");
        }
    }
    

    C2DMReceiver.java(您需要 c2dm.jar file 并将其添加到您的库中)

    package YOURPACKAGE;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Vibrator;
    import android.util.Log;
    
    import com.google.android.c2dm.C2DMBaseReceiver;
    import com.google.android.c2dm.C2DMessaging;
    
    public class C2DMReceiver extends C2DMBaseReceiver 
    {
        public static final String TAG = "YOUR_APP_NAME";
        public static String lastMessage = "";
        public static List<Integer> lastNotifications = new ArrayList<Integer>();
        public static Boolean isForegrounded = true;
    
        public C2DMReceiver()
        {
            //send the email address you set up earlier
            super(DeviceRegistrar.SENDER_ID);
        }
    
        @Override
        public void onRegistered(Context context, String registrationId) throws IOException 
        {
            Log.d(TAG, "successfully registered with C2DM server; registrationId: " + registrationId);
            DeviceRegistrar.registerWithServer(context, registrationId);
        }
    
        @Override
        public void onError(Context context, String errorId) 
        {
            //notify the user
            Log.e(TAG, "error with C2DM receiver: " + errorId);
            if ("ACCOUNT_MISSING".equals(errorId)) {
                //no Google account on the phone; ask the user to open the account manager and add a google account and then try again
                //TODO  
                } else if ("AUTHENTICATION_FAILED".equals(errorId)) {
                    //bad password (ask the user to enter password and try.  Q: what password - their google password or the sender_id password? ...)
                    //i _think_ this goes hand in hand with google account; have them re-try their google account on the phone to ensure it's working
                    //and then try again
                    //TODO  
                } else if ("TOO_MANY_REGISTRATIONS".equals(errorId)) {
                    //user has too many apps registered; ask user to uninstall other apps and try again
                    //TODO  
                } else if ("INVALID_SENDER".equals(errorId)) {
                    //this shouldn't happen in a properly configured system
                    //TODO: send a message to app publisher?, inform user that service is down  
                } else if ("PHONE_REGISTRATION_ERROR".equals(errorId)) {
                    //the phone doesn't support C2DM; inform the user
                    //TODO  
                } //else: SERVICE_NOT_AVAILABLE is handled by the super class and does exponential backoff retries
            }
        }
    
        @Override
        protected void onMessage(Context context, Intent intent) 
        {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                //parse the message and do something with it.
                //For example, if the server sent the payload as "data.message=xxx", here you would have an extra called "message"
                String message = extras.getString("message");
                Log.i(TAG, "received message: " + message);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多