Android8.0 通知栏不显示、无横幅通知问题解决

不知不觉安卓都快9.0了(更新的可够快的),随着它的更新会出现许多新的问题和新的知识,所以我们要不停地学习以及“更新自己”

追加2019-07-18 无横幅通知解决:最近发现大家反馈直接使用代码只能实现图二效果。然后楼主查阅了很多资料,然而都无法很好解决问题。之后研究了一番(诺基亚、华为、小米)小米和华为不能弹出,猜测原因可能是系统版本过高或厂家自定义了系统。因为IM接入时,消息通知问题也接入小米和华为的专用sdk。废话不多说,直接上解决办法,下滑到 步骤3

8.0通知栏效果图如下:

  

1、先说明为什么NotificationCompat.Builder()过时问题,在安卓8.0 “O” 对通知栏新增了一个ChannelId参数,在new的时候添加上channelId即可

new NotificationCompat.Builder(this,"default")

2、为什么以前通知栏代码在8.0无法显示,8.0应该怎样修改才能显示通知信息,下面为大家解答

步骤:

1、每次进行通知触发时,对系统版本进行判断,8.0及以上需要设置好“channelId”(没有特殊要求、唯一即可)、“channelName”(用户看得到的信息)、“importance”(重要等级)这三个重要参数,然后创建到NotificationManager

2、常规方式触发即可、区别在于NotificationCompat.Builder()时加上前面设置的“channelId”

代码如下:

public class MainActivity extends AppCompatActivity{

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

		Button button = (Button) findViewById(R.id.button);

		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				notification();
				finish();
			}
		});
	}

	private void notification() {
		Intent intent = new Intent(this, NotificationIntent.class);
		NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         //8.0 以后需要加上channelId 才能正常显示
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
			String channelId = "default";
			String channelName = "默认通知";
			manager.createNotificationChannel(new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH));
		}

		//设置TaskStackBuilder
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		stackBuilder.addParentStack(NotificationIntent.class);
		stackBuilder.addNextIntent(intent);

		PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

		Notification notification = new NotificationCompat.Builder(this, "default")
				.setSmallIcon(R.mipmap.ic_launcher)
				.setContentTitle("标题")
				.setContentText("这是内容,点击我可以跳转")
				.setAutoCancel(true)
				.setDefaults(Notification.DEFAULT_ALL)
				.setWhen(System.currentTimeMillis())
				.setContentIntent(pendingIntent)
				.build();

		manager.notify(1, notification);
	}

 TaskStackBuilder 作用是打开跳转页面点击返回时回到指定页面,可以参考我之前的博客 Notification通知栏设置及其点击响应

    <activity android:name=".NotificationIntent"
            android:parentActivityName=".MainActivity"/>

Kotlin代码如下

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener{
            notification()
            finish()
        }
    }

    fun notification() {
        val intent = Intent(this,NotificationIntent::class.java)
        val manager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //8.0 以后需要加上channelId 才能正常显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val channelId = "default"
            val channelName = "默认通知"
            manager.createNotificationChannel(NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH))
        }

        val stackBuilder = TaskStackBuilder.create(this)
        stackBuilder.addParentStack(NotificationIntent::class.java)
        stackBuilder.addNextIntent(intent)

        val pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)

        val notification = NotificationCompat.Builder(this,"default")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("标题")
                .setContentText("这是内容,点击我可以跳转")
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .build()

        manager.notify(1,notification)
    }

 3、安卓6.0及以上部分手机消息无横幅通知

关键点在,开启悬浮窗权限

  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //判断是否开启了悬浮通知栏权限   注:部分华为无效
            if (!Settings.canDrawOverlays(this)) {
                getOverlayPermission();
            }
        }

 //请求悬浮窗权限
    @TargetApi(Build.VERSION_CODES.M)
    private void getOverlayPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, 0);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //....自行做回调处理
    }

 加入以上代码解决了小米没有弹窗问题,但是华为还是不行,技术浅薄,还在研究中。。。

赠人玫瑰,手有余香。如果对你有帮助,请大方给个赞!

猜你喜欢

转载自blog.csdn.net/Mr_Leixiansheng/article/details/84942589