Androidの通知バーを使用することを理解して

1.アンドロイド8.0 Notification.Builder事前に通知します、

参考ブログます。https://blog.csdn.net/qi85481455/article/details/82895507

ベースケース:

	  public void sendNotification(View view){
        // 设置点击通知启动  意图
        //   Intent intent = new Intent(this,MainActivity.class);
        Intent intent = new Intent();    // 通过通知意图启动拨打电话界面
        intent.setAction(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:"+110));



        PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


        Notification.Builder builder=new Notification.Builder(this);

        builder.setContentTitle("小标题");
        builder.setContentText("通知内容");
        builder.setSmallIcon(R.mipmap.ic_launcher);   // 小图标
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));  // 大图标
        builder.setWhen(System.currentTimeMillis());  //设置时间
        builder.setAutoCancel(true);   // 点击启动Activity以后自动取消
        builder.setContentIntent(pi);
        Notification nf=builder.build();
        nf.flags= Notification.FLAG_INSISTENT;   // 设置以后通知栏不能被取消,不起作用????
        manager.notify(1,nf);    // 1 是通知id 可以根据1  取消通知 
    }

2.通知の後に8.0をandorid

参考郭神のブログ:https://blog.csdn.net/guolin_blog/article/details/79854070

 アンドロイド8.0システム通知の問題は:あなたは通知バースイッチをオフにした場合、通知は、将来的に分類することができない場合、すべての通知が受信されない
  グループ化、8.0が通知するチャネルの概念を導入アンドロイド通知
チャットにメッセージを入れて、チャットメッセージ、サブスクリプションのメッセージを持っているような高い優先度を設定し、通知チャネル、に
、これはメッセージの通知サブスクリプションをオフにすることができ、 
あなたはまた、再通知時間を設定し、右側の通知バーをスライドさせることができる、このカテゴリのスイッチの通知

ベースケース:

package com.notificationdemo.denganzhi;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.opengl.Visibility;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    String  channelId1 = "chat";
    String  channelId2 = "subscribe";
    
    // 1. 创建通知渠道
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            String channelName = "聊天消息";
            /***
             *  通知等级:
             * NotificationManager.IMPORTANCE_HIGH;
             * IMPORTANCE_DEFAULT
             * IMPORTANCE_LOW
             * IMPORTANCE_MIN
             */
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId1, channelName, importance);


            channelName = "订阅消息";
            importance = NotificationManager.IMPORTANCE_LOW;
            createNotificationChannel(channelId2, channelName, importance);
        }
    }
    
    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setShowBadge(true);  // 允许显示角标
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);


    }

    // 2.  发送 聊挺通知 ,使用  channelId1 聊天渠道
    public void sendNotifiction1(View view){
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, channelId1)
                .setContentTitle("收到一条聊天消息")
                .setContentText("今天中午吃什么?")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setNumber(1000)   // 未读消息
                .build();
        manager.notify(1, notification);

    }


    // 3. 发送 订阅通知,使用通知渠道 channelId2
    public void sendNotifiction2(View view){
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, channelId2)
                .setContentTitle("发送一条订阅消息")
                .setContentText("地铁10号线商铺抢购中!!!!")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .build();
        manager.notify(2, notification);

    }
}

問題:ユーザーはチャット通知スイッチを閉じた場合、利用者は、通知、動的な決意の通知バーを使用することはできません

package com.notificationdemo.denganzhi;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.opengl.Visibility;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    String  channelId1 = "chat";


    // 1. 创建通知渠道

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            String channelName = "聊天消息";
            /***
             *  通知等级:
             * NotificationManager.IMPORTANCE_HIGH;
             * IMPORTANCE_DEFAULT
             * IMPORTANCE_LOW
             * IMPORTANCE_MIN
             */
            int importance = NotificationManager.IMPORTANCE_HIGH;
  // 只会调用一次,系统有优化
            createNotificationChannel(channelId1, channelName, importance)
  
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setShowBadge(true);  // 允许显示角标
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);


    }

    public void sendNotifiction3(View view){
        /**
         *   如果 channelId1 通知渠道开关关闭,
         *   比如聊天 功能无法使用
         *   可以通过 Api判断 改通知去掉 开关状态,
         *   如果用户关闭,那么手动去打开
         */
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = manager.getNotificationChannel(channelId1);
            // 获取通知 渠道配置  IMPORTANCE_NONE 表示 关闭通知渠道了
            if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
                startActivity(intent);
                Toast.makeText(this, "请手动将通知打开", Toast.LENGTH_SHORT).show();
            }
        }
    }

}

 

公開された111元の記事 ウォンの賞賛123 ビュー130 000 +

おすすめ

転載: blog.csdn.net/dreams_deng/article/details/105238324