使用自定义视图创建自定义通知

时间:2016-07-14 02:55:20

标签: android notifications remoteview

我想创建一个处理来电的通知

enter image description here

我尝试使用RemoteView但没有成功。也许我无法与之互动。这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            style="Custom Notification Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image"
            android:text="aasdasdasd" />

        <TextView
            android:id="@+id/text"
            style="Custom Notification Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:layout_toRightOf="@id/image"
            android:text="aasdasdasd" />

    </LinearLayout>


    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

这是我的节目通知功能:

 private void customNotification() {
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_custom_notification);


        Intent intent = new Intent(this, CallingActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        remoteViews.setImageViewResource(R.id.image, R.drawable.bank);
        remoteViews.setTextViewText(R.id.title, "Custom notification");
        remoteViews.setImageViewResource(R.id.icon, R.drawable.individual);
        remoteViews.setTextColor(R.id.title, ResourceUtil.getColorId(R.color.black));
        remoteViews.setTextViewText(R.id.text, "This is a custom layout");
        remoteViews.setTextColor(R.id.text, ResourceUtil.getColorId(R.color.black));
        AppWidgetManager.getInstance(this).updateAppWidget(getComponentName(), remoteViews);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSound(defaultSoundUri)
                .setOngoing(true)
                .setContentTitle("Calling")
                .setContentText("Message")
                .setSmallIcon(R.drawable.login_logo)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setContent(remoteViews);


        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


        mNotificationManager.notify(1, builder.build());
    }

有人可以告诉我这个吗?

1 个答案:

答案 0 :(得分:4)

根本不需要自定义通知 - 这是使用标准notification actions的标准通知。

您看到的行为是heads up notification,只有在您使用setFullScreenIntent()(这也是它长时间保持可见状态)和/或高优先级时才会触发并使用铃声或振动。