[embedyt]https://www.youtube.com/watch?v=axcdnRAcqLw[/embedyt]
activity_main.xmlMainActivity.javaApp.javanotification_collapsed.xmlnotification_expanded.xmlNotificationReceiver.javaAndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showNotification" android:text="show" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.965" /> </android.support.constraint.ConstraintLayout>
package com.codinginflow.customnotificationexample; import android.app.Notification; import android.app.PendingIntent; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.RemoteViews; import static com.codinginflow.customnotificationexample.App.CHANNEL_ID; public class MainActivity extends AppCompatActivity { private NotificationManagerCompat notificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); notificationManager = NotificationManagerCompat.from(this); } public void showNotification(View v) { RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.notification_collapsed); RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded); Intent clickIntent = new Intent(this, NotificationReceiver.class); PendingIntent clickPendingIntent = PendingIntent.getBroadcast(this, 0, clickIntent, 0); collapsedView.setTextViewText(R.id.text_view_collapsed_1, "Hello World!"); expandedView.setImageViewResource(R.id.image_view_expanded, R.drawable.lotti2); expandedView.setOnClickPendingIntent(R.id.image_view_expanded, clickPendingIntent); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_android) .setCustomContentView(collapsedView) .setCustomBigContentView(expandedView) //.setStyle(new NotificationCompat.DecoratedCustomViewStyle()) .build(); notificationManager.notify(1, notification); } }
package com.codinginflow.customnotificationexample; import android.app.Application; import android.app.NotificationChannel; import android.app.NotificationManager; import android.os.Build; public class App extends Application { public static final String CHANNEL_ID = "exampleChannel"; @Override public void onCreate() { super.onCreate(); createNotificationChannel(); } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "Example Channel", NotificationManager.IMPORTANCE_HIGH ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="64dp" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/text_view_collapsed_1" style="@style/TextAppearance.Compat.Notification.Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Message!" android:textColor="@color/colorPrimary" /> <TextView android:id="@+id/text_view_collapsed_2" style="@style/TextAppearance.Compat.Notification.Info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Expand to show!" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="256dp" android:orientation="vertical"> <TextView android:id="@+id/text_view_expanded" style="@style/TextAppearance.Compat.Notification" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_red_light" android:padding="8dp" android:text="This is a custom notification" android:textAlignment="center" android:textColor="#FFF" /> <ImageView android:id="@+id/image_view_expanded" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/lotti" /> </LinearLayout>
package com.codinginflow.customnotificationexample; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationManagerCompat; import android.widget.Toast; public class NotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Image clicked", Toast.LENGTH_SHORT).show(); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.cancel(1); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.codinginflow.customnotificationexample"> <application android:name=".App" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".NotificationReceiver" /> </application> </manifest>