您的位置:首页 > 移动开发 > Android开发

[android]JPush自定义通知栏

2015-06-24 16:28 465 查看

JPush嵌入工程

官网详解

自定义通知栏

先自定义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());
	}


我试图更改下来通知栏里的icon,各种方法都试过,文档看了无数遍,发现这个图标是不可更改的,永远和APP程序图标一样的。。



带有下拉效果

还有一种自定义通知栏,带有下拉效果。是官网介绍的。

布局文件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);


这里面有一个方法builder.statusBarDrawable,这个设置的是一个状态栏图标,不是下拉界面里那个。当来通知时,手机顶部会滚动提示一下来消息了。这里设置的图标就是刚来通知在顶部提示的,而不是上图里红框里那个。

这种带下来效果



官网提供的一种简版

还有一种简版的,也是官网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,发送时指定编号发送。



JPush官网android API
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: