首页 > 代码库 > =====umeng--->push======
=====umeng--->push======
<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=http://www.mamicode.com/"Umeng" >>
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); } }); } }自定义notification样式的话必须有:
=====umeng--->push======
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。