标签:android
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.day_2015_umengpush" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11" /> <!-- umengpush ========================================================== --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <!-- 【可选】根据需要添加 --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!-- umengpush ========================================================== --> <application android:name="com.panpass.main.MyApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.panpass.main.MainActivity" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.panpass.main.OpenActivity" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:screenOrientation="portrait" > </activity> <service android:name="com.panpass.pushservice.PushService" /> <!-- umengpush ========================================================== --> <meta-data android:name="UMENG_CHANNEL" android:value="Umeng" > </meta-data> <meta-data android:name="UMENG_APPKEY" android:value="54b778aefd98c523200005f6" > </meta-data> <meta-data android:name="UMENG_MESSAGE_SECRET" android:value="e75bee338ac1123f7f46260cfc2828db" > </meta-data> <receiver android:name="com.umeng.message.NotificationProxyBroadcastReceiver" android:exported="false" > </receiver> <receiver android:name="com.umeng.message.RegistrationReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> <!-- 【可选】根据需要添加 --> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <receiver android:name="com.umeng.message.UmengBroadcastReceiver" > <intent-filter> <action android:name="org.agoo.android.intent.action.RECEIVE" /> </intent-filter> <intent-filter> <action android:name="com.example.day_2015_umengpush.intent.action.COMMAND" /> </intent-filter> <intent-filter> <action android:name="org.agoo.android.intent.action.RE_ELECTION_V2" /> </intent-filter> </receiver> <service android:name="com.umeng.message.UmengService" android:exported="true" android:process=":umengService_v1" > <intent-filter> <action android:name="com.example.day_2015_umengpush.intent.action.START" /> </intent-filter> <intent-filter> <action android:name="com.example.day_2015_umengpush.intent.action.COCKROACH" /> </intent-filter> <intent-filter> <action android:name="org.agoo.android.intent.action.PING" /> </intent-filter> </service> <service android:name="org.android.agoo.service.ElectionService" android:exported="true" android:process=":umengService_v1" > <intent-filter> <action android:name="org.agoo.android.intent.action.ELECTION_V2" /> </intent-filter> </service> <service android:name="com.umeng.message.UmengIntentService" /> <!-- V1.3.0添加的service,负责下载通知的资源 --> <service android:name="com.umeng.message.UmengDownloadResourceService" /> <!-- umengpush ========================================================== --> </application> </manifest>
package com.panpass.main; import com.example.day_2015_umengpush.R; import com.umeng.message.PushAgent; import com.umeng.message.UTrack; import com.umeng.message.UmengMessageHandler; import com.umeng.message.UmengNotificationClickHandler; import com.umeng.message.entity.UMessage; import android.app.Application; import android.app.Notification; import android.content.Context; import android.os.Handler; import android.support.v4.app.NotificationCompat; import android.widget.RemoteViews; import android.widget.Toast; public class MyApplication extends Application { private PushAgent mPushAgent = null; @Override public void onCreate() { mPushAgent = PushAgent.getInstance(this); mPushAgent.setDebugMode(true); UmengMessageHandler umh = new UmengMessageHandler(){ @Override public void dealWithCustomMessage(final Context context, final UMessage msg) { new Handler(getMainLooper()).post(new Runnable() { @Override public void run() { UTrack.getInstance(getApplicationContext()).trackMsgClick(msg); Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show(); } }); } @Override public Notification getNotification(Context context, UMessage msg) { switch (msg.builder_id) { case 1: NotificationCompat.Builder builder = new NotificationCompat.Builder(context); RemoteViews myNotificationView = new RemoteViews(context.getPackageName(), R.layout.notification_view); myNotificationView.setTextViewText(R.id.notification_title, msg.title); myNotificationView.setTextViewText(R.id.notification_text, msg.text); myNotificationView.setImageViewBitmap(R.id.notification_large_icon, getLargeIcon(context, msg)); myNotificationView.setImageViewResource(R.id.notification_small_icon, getSmallIconId(context, msg)); builder.setContent(myNotificationView); Notification mNotification = builder.build(); //由于Android v4包的bug,在2.3及以下系统,Builder创建出来的Notification,并没有设置RemoteView,故需要添加此代码 mNotification.contentView = myNotificationView; return mNotification; default: //默认为0,若填写的builder_id并不存在,也使用默认。 return super.getNotification(context, msg); } } }; mPushAgent.setMessageHandler(umh); /** * 该Handler是在BroadcastReceiver中被调用,故 * 如果需启动Activity,需添加Intent.FLAG_ACTIVITY_NEW_TASK * 实际上就是回调 * */ UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler(){ @Override public void dealWithCustomAction(Context context, UMessage msg) { Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show(); } }; mPushAgent.setNotificationClickHandler(notificationClickHandler); } }
package com.panpass.main; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; import com.example.day_2015_umengpush.R; import com.umeng.message.PushAgent; import com.umeng.message.UmengRegistrar; public class MainActivity extends Activity { private TextView mTextView; private PushAgent mPushAgent = null; private static final int GET_DEVICE_TOKEN = 1; private Handler mHandler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case GET_DEVICE_TOKEN: mTextView.setText(String.valueOf(msg.obj)); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.text_view); // // PushAgent mPushAgent = PushAgent.getInstance(this); // mPushAgent.enable(); // PushAgent.getInstance(this).onAppStart(); // String device_token = UmengRegistrar.getRegistrationId(this); // startService(new Intent(this,PushService.class)); mPushAgent = PushAgent.getInstance(this); PushAgent.getInstance(this).onAppStart(); mPushAgent.enable(); getToken(); } private void getToken(){ mHandler.post(new Runnable() { @Override public void run() { String device_token = UmengRegistrar.getRegistrationId(MainActivity.this); Message msg = mHandler.obtainMessage(); msg.what = GET_DEVICE_TOKEN; msg.obj = device_token; mHandler.sendMessage(msg); } }); } }
标签:android
原文地址:http://blog.csdn.net/soulofandroid/article/details/42775679