2012-08-12 142 views
1

我正在使用Phonegap & jQuery Mobile構建Android應用程序。我想實現推送通知,我發現像一些方法:Urban-Air & Phonegap PluginsAndroid的Phonegap推送通知

但他們似乎並不支持科爾多瓦1.9 ......那麼,有沒有其他的,我可以使用新版本?

+1

我不確定你是否正確使用此功能,但是我的博客涵蓋了android的通知。您不需要任何付費選項。 http://programmersgabble.blogspot.com/ – Jed 2012-09-18 14:17:59

回答

2

你可以從Pushwoosh嘗試推SDK:http://pushwoosh.com,他們都是免費的,已經有1.9科爾多瓦和GCM支持(不像UrbanAirship)。

1

城市飛艇如果付費服務和提供的插件僅適用於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); 
     } 
    } 
}