标签:
先自定义Receiver
<receiver android:name="Your Receiver" android:enabled="true"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <category android:name="You package Name" /> </intent-filter> </receiver>
private static final String TAG = "MyReceiver"; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { Log.i(TAG, "JPush用户注册成功"); } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent .getAction())) { Log.i(TAG, "接受到推送下来的自定义消息"); receivingNotification(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent .getAction())) { Log.i(TAG, "接受到推送下来的通知"); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent .getAction())) { Log.i(TAG, "用户点击打开了通知"); openNotification(context, bundle); } }在receivingNotification()中自定义notification通知栏
private void receivingNotification(Context context, Bundle bundle) { NotificationManager manager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); String message = bundle.getString(JPushInterface.EXTRA_MESSAGE); String extras = bundle.getString(JPushInterface.EXTRA_EXTRA); // 使用notification // 使用广播或者通知进行内容的显示 NotificationCompat.Builder builder = new NotificationCompat.Builder( context); builder.setContentText(message).setSmallIcon(R.drawable.notification_icon_2).setContentTitle(JPushInterface.EXTRA_TITLE); builder.setDefaults(Notification.DEFAULT_SOUND); manager.notify(1,builder.build()); }
还有一种自定义通知栏,带有下拉效果。是官网介绍的。
布局文件customer_notitfication_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginLeft="10dip" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#286CA6" android:textSize="20sp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#6FA45E" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder( MainActivity.this, R.layout.customer_notitfication_layout, R.id.icon, R.id.title, R.id.text); // 指定定制的 Notification Layout builder.statusBarDrawable = R.drawable.notification_icon; // 指定最顶层状态栏小图标 builder2.layoutIconDrawable = R.drawable.notification_icon; // 指定下拉状态栏时显示的通知图标 JPushInterface.setPushNotificationBuilder(1, builder1);
这种带下来效果
还有一种简版的,也是官网demo,mark一下。设置成默认,发送通知
BasicPushNotificationBuilder builder2 = new BasicPushNotificationBuilder( MainActivity.this); builder2.statusBarDrawable = R.drawable.logo; builder2.notificationFlags = Notification.FLAG_AUTO_CANCEL; // 设置为自动消失 builder2.notificationDefaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS; // 设置为铃声与震动都要 JPushInterface.setDefaultPushNotificationBuilder(builder2); JPushInterface.setPushNotificationBuilder(2, builder2);
setPushNotificationBuilder(2, builder2);设置builder2的样式编号为2,发送时指定编号发送。
标签:
原文地址:http://blog.csdn.net/bless2015/article/details/46623569