I think there are many restrictions on custom notification options for Android. The official API tells us that it still has to set three options: icon, tittle, and text. In addition, you must set pengdingintent, many netizens also reflected builder. setContent (remoteViews) must be followed by builder. setContentIntent (pendingIntent), or an error: android. app. remoteServiceException: Bad notification posted from package.
MainActivity code
Package com. example. f04_notification; import android. OS. bundle; import android. app. activity; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. context; import android. content. intent; import android. support. v4.app. notificationCompat; import android. view. view; import android. widget. button; import android. widget. remoteViews; public class MainActivity extends Activit Y {private icationicationmanager manager; private Button button; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button1); // obtain the System Service manager = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE); button. setOnClickListener (new View. onClickListener () {@ Overridep Ublic void onClick (View v) {// TODO Auto-generated method stubicationicationcompat. builder builder = new NotificationCompat. builder (MainActivity. this); RemoteViews remoteViews = new RemoteViews (getPackageName (), R. layout. notifiaction); // due to restrictions of the android api, set small icon, setContentTittle, and setContentTextbuilder are still required for custom notifications. setSmallIcon (R. drawable. ic_launcher); builder. setContentText ("hello world! "); Builder. setContentTitle ("Android"); // you must set remoteViews again. setTextViewText (R. id. textView1, "Notification coming"); remoteViews. setImageViewResource (R. id. imageView1, R. drawable. ic_launcher); Intent intent = new Intent (MainActivity. this, MainActivity. class); // the flags of pendingintent are divided into four FLAG_CANCEL_CURRENT, FLAG_NO_CREATE, FLAG_ONE_SHOT, FLAG_UPDATE_CURRENTPendingIntent pendingIntent = PendingIntent. getActivity (MainActivity. this, 1, intent, PendingIntent. FLAG_ONE_SHOT); builder. setContent (remoteViews); builder. setContentIntent (pendingIntent); manager. using Y (int) System. currentTimeMillis (), builder. build ());}});}}
Layout xml files
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView1" android:contentDescription="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="27dp" android:layout_marginTop="33dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView1" android:layout_marginLeft="76dp" android:layout_toRightOf="@+id/imageView1" android:text="@string/hello_world" /></RelativeLayout>
Thank you for your reading. I hope you can learn from each other and make progress together!