通过remoteViews自定义Notification通知栏的布局

remoteViews,是一种远程view,通过跨进程更新自己的界面,主要用于通知栏和桌面小部件的开发过程中。

1、通知栏的实现

public class MainActivity extends Activity implements View.OnClickListener {

    private Button btn_one,btn_two,btn_three,btn_four;
    private NotificationManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_one = (Button) findViewById(R.id.btn_one);
        btn_one.setOnClickListener(this);
        btn_two = (Button) findViewById(R.id.btn_two);
        btn_two.setOnClickListener(this);
        btn_three = (Button) findViewById(R.id.btn_three);
        btn_three.setOnClickListener(this);
        btn_four = (Button) findViewById(R.id.btn_four);
        btn_four.setOnClickListener(this);
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_one://默认通知
                PendingIntent pendingIntent1=PendingIntent.getActivity(this,0,new Intent(this,OtherActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
                Notification notification1=new Notification();
                notification1.icon=R.drawable.head;
                notification1.when=System.currentTimeMillis();
                notification1.tickerText="您有新短消息,请注意查收!";
                notification1.contentIntent=pendingIntent1;
                manager.notify(1,notification1);

                break;
            case R.id.btn_two://API11之后使用
                PendingIntent pendingIntent2=PendingIntent.getActivity(this,0,new Intent(this,OtherActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
                Notification notification2=new Notification.Builder(this)
                        .setSmallIcon(R.drawable.head)//即便自定义了图标,但有时候还是显示系统默认的,应该与手机类型有关
                        .setContentTitle("好消息")
                        .setContentText("API11之后的通知")
                        .setTicker("hello world")
                        .setWhen(System.currentTimeMillis())
                        .setContentIntent(pendingIntent2)
                        .setNumber(1)       //在最右侧现实。这个number起到一个序列号的作用,如果多个触发多个通知(同一ID),可以指定显示哪一个。
                        .getNotification(); //build()是在API16及之后增加的,在API11中可以使用getNotificatin()来代替
                manager.notify(1,notification2);
                break;
            case R.id.btn_three://API16之后使用
                PendingIntent pendingIntent3=PendingIntent.getActivity(this,0,new Intent(this,OtherActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
                Notification notification3=new Notification.Builder(this)
                        .setSmallIcon(R.drawable.head)
                        .setContentTitle("好消息")
                        .setContentText("API16之后的通知")
                        .setTicker("hello world")
                        .setWhen(System.currentTimeMillis())
                        .setContentIntent(pendingIntent3)
                        .setNumber(1)
                        .build();
                manager.notify(1,notification3);
                break;
            case R.id.btn_four://自定义通知,
                Notification notification = new Notification();
                notification.when = System.currentTimeMillis();
                notification.flags = Notification.FLAG_AUTO_CANCEL;
                notification.tickerText = "hello world";
                notification.icon = R.drawable.head;//这是个坑,如果不设置icon,通知不显示

                Intent intent = new Intent(MainActivity.this, OtherActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remote_layout);
                remoteViews.setTextViewText(R.id.tv_title, "请假条");
                remoteViews.setTextViewText(R.id.tv_content, "世界这么大,我想去看看");
                remoteViews.setImageViewResource(R.id.iv_head, R.drawable.head);

                notification.contentIntent = pendingIntent;
                notification.contentView = remoteViews;
                manager.notify(1, notification);
                break;
        }
    }
}

通知栏的布局remote_layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_head"
        android:layout_width="40dp"
        android:layout_height="40dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="标题" />

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="3dp"
            android:text="内容。。。。" />
    </LinearLayout>
</LinearLayout>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Firebase 推送消息可以通过自定义通知来展示更加个性化的消息。 首先,在 Firebase 控制台中,你需要为你的应用程序创建一个自定义通知通道。在创建通道时,你可以指定通知的名称、描述和重要性级别等信息。 接下来,你需要在你的 Android 应用程序中创建一个自定义通知布局。你可以使用一个 XML 文件来定义布局,其中包含你想要显示的所有文本、图像和其他元素。然后,在接收到 Firebase 消息时,你可以使用 RemoteViews 类将自定义布局通知相关联。 最后,在接收到 Firebase 消息时,你需要在代码中使用 NotificationCompat.Builder 类构建自定义通知。在构建通知时,你可以指定标题、文本和自定义布局等信息。最后,使用 NotificationManagerCompat 类将通知发送到系统通知中。 以下是一个示例代码片段,可以帮助你开始构建自定义通知: ``` // 创建自定义通知通道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); channel.setDescription(channelDescription); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // 创建自定义通知布局 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout); contentView.setTextViewText(R.id.title, notificationTitle); contentView.setTextViewText(R.id.text, notificationText); // 构建自定义通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.notification_icon) .setCustomContentView(contentView) .setPriority(NotificationCompat.PRIORITY_HIGH); // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); ``` 注意:在使用自定义通知时,你需要确保你的应用程序有足够的权限来发送通知。如果你的应用程序没有这些权限,则无法正常发送通知

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值