Detailed Notification Drawer Notification Notice for Android

Keywords: Android simulator




// Learn the content of this chapter you need to know two important points of knowledge content!

1. Problems that Custom Notification does not display
Solution:
(1) Forget notify() notification method
(2) Set icon icon

2. If the API is 16, replace getNotification() with build()
2. Pending Intent has a getActivity method

Pending Intent has a getActivity method, the first parameter is context, the second parameter requestCode, the third parameter is Intent, which is used to store information, and the fourth parameter is the operation identification of parameters, commonly used are FLAG_CANCEL_CURRENT and FLAG_UPDATE_CURRENT.
When using FLAG_UPDATE_CURRENT:
Pending Intent. getActivity (context, 0, notificationIntent, Pending Intent. FLAG_CANCEL_CURRENT);

FLAG_UPDATE_CURRENT updates previous Pending Intent messages. For example, you push Message 1 and put Extra in Intent with a value of "ABC". Before you click on the message, you continue to push the second message and Extra in Intent with a value of "CBA". Well, if you click Message 1 or Message 2, you will find that they are both Inte. The information read by NT is "CBA". That is to say, the second replaces the first.

When using FLAG_CANCEL_CURRENT:

Still the above operation steps, this time you will find that when you click on message 1, there is no response, the second can be clicked.

The reason for the above two problems is that the second parameter requestCode, when the value of requestCode is the same, the latter will work on the previous message, so in order to avoid affecting the previous message, requestCode sets different content each time.

Direct Up Source:
The code is long, but the comments are detailed:

package com.example.notifications;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //Method of Sending a Common View Notification
    public void button1(View v) {
        //
        Notification.Builder Builder=new Notification.Builder(this);
        //Set up a small icon
        Builder.setSmallIcon(R.drawable.ic_launcher);
        //Set title
        Builder.setContentTitle("I'm a regular notification drawer.");
        //Setting Content Information
        Builder.setContentInfo("I can open a new interface by clicking");
        //Set up resident notification, pay attention to resident notification and automatic clearance, it is better not to use together
    //  Builder.setOngoing(true);
        //Set Click Notification to Clear automatically
        Builder.setAutoCancel(true);
        /*
         * Set the sound of notification + vibration + breathing lamp, pay attention!!! Setting this value will increase the power of system vibration.
         * <uses-permission android:name="android.permission.VIBRATE"></uses-permission>
         * Otherwise, the simulator will abnormally exit the program.
         */
        Builder.setDefaults(Notification.DEFAULT_ALL);
        //Settings notified to which message
        Builder.setNumber(10);
        //Set the text to notify the drawer when it has not been pulled down
        Builder.setTicker("New news");

        /*
         *   Set up a click event for drawer notification
         *  Define an intention to open an activity when opening a notification;
         *  getActivities Parameters:
         *  Parameter 1 (context) parameter 2 (notification request code), parameter 3 (encapsulated Intent object) parameter 4 (a tag)
         *  Open an Activity through getActivities
         */
        Intent intent=new Intent(this,open_Activity.class);
        PendingIntent pi=PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        Builder.setContentIntent(pi);//The parameter pi is a pending Intent intent object

        //Start building the set message as a notification object
        Notification n=Builder.build();
        //Get the notification manager of the system and send messages through the manager of the system
        NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        //Send notifications, parameters (the number of a notification, the notification object to be sent)
        nm.notify(1,n);
    }

    //Method of sending a large view notification
    @SuppressLint("NewApi")
    public void button2(View v) {
        Notification.Builder Builder=new Notification.Builder(this);
        //Set up a small icon
        Builder.setSmallIcon(R.drawable.ic_launcher);
        //Set title
        Builder.setContentTitle("I'm a big view notification drawer.");
        //Setting Content Information
        Builder.setContentInfo("Welcome to use Notification Drawer");


        //Set the style of the big view!!!
        Notification.InboxStyle style=new Notification.InboxStyle();
        //Set the title of the content of the large view
        style.setBigContentTitle("Dandelion Mother");
        //Set notes
        style.setSummaryText("Author: Wenlong");
        //Set the text for each line
        style.addLine("Dandelion Mothers have parachutes for their children");
        style.addLine("As long as the wind blows gently");
        style.addLine("The child drifted away in the wind.");

        //Set the defined style to the notification
        Builder.setStyle(style);

        Notification nf =Builder.build();

        NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(2,nf);
    }

    //Method of sending a progress bar view notification
        public void button3(View v) {
            final Notification.Builder Builder=new Notification.Builder(this);
            //Set up a small icon
            Builder.setSmallIcon(R.drawable.ic_launcher);
            //Set title
            Builder.setContentTitle("I am a progress note");
            //Setting Content Information
            Builder.setContentInfo("Simulated Download");

            //Build a notification manager
            final NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


            //Simulated download threads
            new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    for(int prograss=0;prograss<=100;prograss+=5) {
                        //Parameters (Maximum Progress, Current Progress, Uncertain Progress)
                        Builder.setProgress(100,prograss,false);
                        //Send notifications every time you follow the new completion schedule.
                        nm.notify(3,Builder.build());

                        //Sleep 500 milliseconds for every five plus
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                    //After the download is completed:
                    Builder.setProgress(0,0, false);
                    Builder.setContentText("Simulated download completed!!!");

                    nm.notify(3, Builder.build());
                    //Finally, don't forget!!!!!!! Thread start()
                }
            }).start();;
        }


        //Method of sending a custom view notification
        public void button4(View v) {
            Notification.Builder Builder=new Notification.Builder(this);

            //!!!!! Keep in mind that if you don't set a small icon, custom notifications won't show up.

            //Set up a small icon
            Builder.setSmallIcon(R.drawable.ic_launcher);
            /*
             * RemoteViews(Chinese Identity Logo Remote Opinion)
             * Create a remote view parameter 1 (customize the package name of the Layout view layout file and the ID of the Layout view layout file)
             */
            RemoteViews views=new RemoteViews(getPackageName(),R.layout.notice_layout);

            //In this way, text can be set to a TextView's id or Button's id.
            views.setTextViewText(R.id.tv1,"Sea laugh");

            //This method can set the click event for the button, but I don't know the specific usage yet. Now comment it out.
    //      views.setOnClickPendingIntent(viewId, pendingIntent);



            //Set the remote view to the notification through the setContent method
            Builder.setContent(views);

            Builder.setTicker("I customized a notification(Wenlong Player)");

            //By, notification manager sends notifications
            NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(4, Builder.build());
            Toast.makeText(getApplicationContext(), "hello",1000).show();
        }
}

Posted by beckjo1 on Sun, 26 May 2019 12:58:45 -0700