当前位置: 首页 > 工具软件 > urban-theme > 使用案例 >

Urban Airship Android Client - Helium Push

朱兴运
2023-12-01

本文链接:http://blog.csdn.net/kongxx/article/details/8176986


今天说说怎样来创建一个最简单的可以接收Urban AirShip通知的Android应用程序。

1. 首先创建一个Android应用,这里需要注意的是Android应用的package名必须和Urban AirShip上application的package名一致。

2. 添加urbanairship-lib-2.0.1.jar到libs目录下,并将其加入工程的classpath中。

3. 修改AndroidManifest.xml文件,内容类似如下

<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文件,内如如下:
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文件,先加入几个值,后面会用到

<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文件要在后面才添加,如果有提示错误先忽略)

<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
<?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

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类

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。
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();
		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);
	}
}
8. 测试

8.1首先运行上面创建的Android应用程序,并从日中获取APID。

8.2 然后从Urban AirShip上给此APID发送notification。

8.3 稍等片刻,会在Andorid模拟器的状态栏上接收到notification。










 类似资料:

相关阅读

相关文章

相关问答