2.android手机通知开发

package cn.fc.notificationtest;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

/**
 *  小米4手机可以使用,nexus手机不能使用
 * */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    

    private final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = (Button)findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
    
    
        switch (v.getId()){
    
    
            case R.id.send_notice:
                NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(this).setContentTitle("This is content title").
                        setContentText("This is content text").setWhen(System.currentTimeMillis()).
                        setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).build();
                Log.d(TAG, "notificationING");
                manager.notify(1, notification);
                break;
            default:
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/esabeny/article/details/113058867