Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Urban Airship Android Client - Helium Push

Urban Airship Android Client - Helium Push

編輯:關於Android編程


今天說說怎樣來創建一個最簡單的可以接收Urban AirShip通知的Android應用程序。

1. 首先創建一個Android應用,這裡需要注意的是Android應用的package名必須和Urban AirShip上application的package名一致。

2. 添加urbanairship-lib-2.0.1.jar到libs目錄下,並將其加入工程的classpath中。

3. 修改AndroidManifest.xml文件,內容類似如下


[html] 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="urbanairship.client" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk 
        android:minSdkVersion="8" 
        android:targetSdkVersion="15" /> 
 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.BROADCAST_STICKY" /> 
 
    <application 
        android:name="urbanairship.client.MyApplication" 
        android:enabled="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" > 
        <activity 
            android:name=".MainActivity" 
            android:label="@string/title_activity_main" > 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
         
        <receiver android:name="com.urbanairship.CoreReceiver" > 
            <intent-filter> 
                <action android:name="android.intent.action.BOOT_COMPLETED" /> 
                <action android:name="android.intent.action.ACTION_SHUTDOWN" /> 
            </intent-filter> 
        </receiver> 
 
        <service 
            android:name="com.urbanairship.push.PushService" 
            android:label="Push Notification Service" 
            android:process=":com.urbanairship.process" /> 
        <service 
            android:name="com.urbanairship.push.PushWorkerService" 
            android:label="Push Notification Worker Service" 
            android:process=":com.urbanairship.process" /> 
        <service 
            android:name="com.urbanairship.analytics.EventService" 
            android:label="Event Service" 
            android:process=":com.urbanairship.process" /> 
 
        <service android:name="com.urbanairship.push.PushService" /> 
 
        <receiver android:name="urbanairship.client.IntentReceiver" /> 
 
        <provider 
            android:name="com.urbanairship.UrbanAirshipProvider" 
            android:authorities="urbanairship.client.urbanairship.provider" 
            android:exported="false" 
            android:multiprocess="true" /> 
    </application> 
 
</manifest> 
4. 在assets目錄下創建airshipconfig.properties文件,內如如下:
[plain] 
developmentAppKey = Your Development App Key 
developmentAppSecret = Your Development App Key 
productionAppKey = Your Production App Key 
productionAppSecret = Your Production Secret 
 
#transport is "gcm" or "helium". 
transport = helium 
 
#gcmSender = GCM sender ID 
#Your GCM sender ID is your Google API project ID (required for GCM) 
 
inProduction = false 
iapEnabled = false 
 
# 2 = Log.VERBOSE; 3 = Log.DEBUG; 4 = Log.INFO; 
# 5 = Log.WARN; 6 = Log.ERROR; 7 = Log.ASSERT; 
developmentLogLevel = 3 
productionLogLevel = 6 
注意替換其中developmentAppKey和developmentAppSecret值(可以從Urban AirShip中創建的Application下獲取)
5. 修改res/values/strings.xml文件,先加入幾個值,後面會用到


[html] 
<resources> 
 
    <string name="app_name">urbanairship-client</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="menu_settings">Settings</string> 
    <string name="title_activity_main">MainActivity</string> 
    <string name="enable_push">Enable Push</string> 
    <string name="notification_subject">Subject</string> 
    <string name="notification_message">Message</string> 
 
</resources> 
6. 修改layout文件,工程中默認創建了一個activity_main.xml,這裡還需要再添加一個layout用來顯示notification。
activity_main.xml (這裡可能會出錯,因為有些java文件要在後面才添加,如果有提示錯誤先忽略)

[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
 
    <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:layout_centerVertical="true" 
        android:text="@string/hello_world" 
        tools:context=".MainActivity" /> 
 
    <CheckBox 
        android:id="@+id/enable_push" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true" 
        android:layout_centerHorizontal="true" 
        android:layout_marginTop="31dp" 
        android:text="@string/enable_push" /> 
 
</RelativeLayout> 
notification.xml
[html] 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:layout_alignParentTop="true" 
    android:layout_height="fill_parent"> 
     
  <ImageView android:id="@+id/icon" 
      android:contentDescription="" 
      android:layout_width="24dip" 
      android:layout_height="24dip" /> 
       
  <TextView android:id="@+id/subject" 
      android:text="@string/notification_subject" 
      android:layout_alignTop="@+id/icon" 
      android:layout_toRightOf="@+id/icon" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:maxLines="1" /> 
 
  <TextView android:id="@+id/message" 
      android:textSize="14dip" 
      android:textColor="#FF000000" 
      android:text="@string/notification_message" 
      android:maxLines="1" 
      android:layout_marginTop="3dip" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/subject" 
      android:layout_width="wrap_content" /> 
 
</RelativeLayout> 
7. 現在開始處理Java代碼了
7.1 首先在MainActivity.java中添加一個checkbox,用來顯式的處理enable push功能,並且在選中是enable push功能,並打印出APID,這裡APID可以在日志中看到,後面需要此APID用來從Urban AirShip發送消息。

MainActivity.java


[java]
package urbanairship.client; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.CheckBox; 
 
import com.urbanairship.push.PushManager; 
import com.urbanairship.push.PushPreferences; 
 
public class MainActivity extends Activity { 
 
    private static final String logTag = MainActivity.class.getName(); 
     
    private CheckBox enablePush; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         
        enablePush = (CheckBox) this.findViewById(R.id.enable_push); 
        enablePush.setEnabled(true); 
        enablePush.setOnClickListener(new OnClickListener() { 
            public void onClick(View v) { 
                if (enablePush.isChecked()) { 
                    PushManager.enablePush(); 
                    PushPreferences pref = PushManager.shared().getPreferences(); 
                    Log.i(logTag, "APID: " + pref.getPushId()); 
                } else { 
                    PushManager.disablePush(); 
                } 
            } 
        }); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.activity_main, menu); 
        return true; 
    } 

7.2 現在開始是接收notification類

[java] 
package urbanairship.client; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
 
import com.urbanairship.UAirship; 
import com.urbanairship.push.PushManager; 
 
import java.util.Set; 
 
public class IntentReceiver extends BroadcastReceiver { 
 
    private static final String logTag = IntentReceiver.class.getName(); 
 
    @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.setClass(UAirship.shared().getApplicationContext(), MainActivity.class); 
            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)); 
        } else if (action.equals(PushManager.ACTION_GCM_DELETED_MESSAGES)) { 
            Log.i(logTag, "The GCM service deleted " + intent.getStringExtra(PushManager.EXTRA_GCM_TOTAL_DELETED) + " messages."); 
        } 
    } 
 
    private void logPushExtras(Intent intent) { 
        Set<String> keys = intent.getExtras().keySet(); 
        for (String key : keys) { 
            Log.i(logTag, "Push Notification Extra: [" + key + " : " + intent.getStringExtra(key) + "]"); 
        } 
    } 

7.3 最後是一個Application類,用來處理並顯示notification。
[java]
package urbanairship.client; 
 
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 MyApplication extends Application { 
 
    @Override 
    public void onCreate() { 
        super.onCreate(); www.2cto.com
        AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this); 
 
        UAirship.takeOff(this, options); 
        Logger.logLevel = Log.VERBOSE; 
 
        CustomPushNotificationBuilder cpnb = new CustomPushNotificationBuilder(); 
        cpnb.statusBarIconDrawableId = R.drawable.ic_launcher; 
        cpnb.layout = R.layout.notification; 
        cpnb.layoutIconDrawableId = R.drawable.ic_launcher;// custom layout icon 
        cpnb.layoutIconId = R.id.icon; 
        cpnb.layoutSubjectId = R.id.subject; 
        cpnb.layoutMessageId = R.id.message; 
 
        PushManager.shared().setNotificationBuilder(cpnb); 
        PushManager.shared().setIntentReceiver(IntentReceiver.class); 
    } 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved