适用于Android的Phonegap推送通知

时间:2012-08-12 11:23:47

标签: jquery-mobile cordova push-notification

我正在使用Phonegap& amp; jQuery Mobile。我想实现推送通知,我找到了一些方法,例如:Urban-Air& Phonegap Plugins

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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