今在Android Studio中使用Notification进行消息推送时,点击消息推送按钮后APP并没有进行消息推送,查看Logcat出现Failed to create image decoder with message 'unimplemented'(使用华为P30 pro真机测试):

Android APP使用Notification无法进行消息推送的解决方案

其中MainActivity.java中代码如下:

package com.example.notificationtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @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 view) {
        switch (view.getId()){
            case R.id.send_notice:
                NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);//NotificationManager实例对通知进行管理
                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();
                Uri soundUri=Uri.fromFile(new File("/system/media/audio/notificationTest/Simple.ogg"));
                //notification.sound=soundUri;
                notification.ledARGB= Color.GREEN;//控制通知的led灯颜色
                notification.ledOnMS=1000;//通知灯的显示时间
                notification.ledOffMS=1000;
                notification.flags=Notification.FLAG_SHOW_LIGHTS;
                manager.notify(1,notification);//调用NotificationManager的notify方法使通知显示
                break;
            default:
                break;
        }
    }
}

activity_main.xml中代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/send_notice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send notice"
        android:layout_gravity="center_vertical|center_horizontal"/>
</LinearLayout>

经过在网上查阅资料,解决方案记录如下:

1、通过使用Log.d()查看了manager和bitmap是否为null,发现bitmap为null,说明BitmapFactory.decoderResource图像解码存在问题,我们将ic_launch图片重命名后再次运行,发现还是无法推送消息。

Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
Log.d("MainActivity",String.valueOf((bitmap==null)));
Log.d("MainActivity",String.valueOf((manager==null)));

Android APP使用Notification无法进行消息推送的解决方案

据查阅网上资料,知晓该android版本需要手动添加NotificationChannel实现,则在manager.notify前添加如下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = new NotificationChannel("AppTestNotificationId", "AppTestNotificationName", NotificationManager.IMPORTANCE_DEFAULT);
    manager.createNotificationChannel(notificationChannel);
    notification.setChannelId("AppTestNotificationId");
}

即可解决问题,这时系统通知栏即显示通知。

相关文章:

  • 2021-06-05
  • 2022-12-23
  • 2022-01-08
  • 2021-12-03
  • 2022-12-23
  • 2021-07-25
  • 2021-09-28
猜你喜欢
  • 2021-12-04
  • 2022-01-12
  • 2021-07-12
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案