前言:在最近学习安卓通知功能的开发中,遇到了一些坑,困扰了我一些时间,搜集了大量资料写个博客总结一下,希望对大家有帮助。
目录
一、启动项目闪退
1.1、问题详情
1.2、解决方法
二、点击通知无法跳转
2.1、问题详情
2.2、解决方法
三、通知功能完整代码
MainActivity
activity_main.xml
NotificationActivity
一、启动项目闪退
1.1、问题详情
完整报错如下:
Caused by: java.lang.IllegalArgumentException: com.example.myapplication: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. at android.app.PendingIntent.checkPendingIntent(PendingIntent.java:461) at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:577) at android.app.PendingIntent.getActivity(PendingIntent.java:563) at android.app.PendingIntent.getActivity(PendingIntent.java:527) at com.example.myapplication.MainActivity.onCreate(MainActivity.java:46) at android.app.Activity.performCreate(Activity.java:8980) at android.app.Activity.performCreate(Activity.java:8958) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1526) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4013) ... 13 more
1.2、解决方法
查阅了大量资料发现在创建PendingIntent的时候因为SDK的版本导致启动app闪退,需要根据不同系统版本创建带有不同flag的PendingIntent,代码如下:
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_ONE_SHOT);
}
二、点击通知无法跳转
2.1、问题详情
点击通知后无法进行跳转。
2.2、解决方法
需要在AndroidManifest.xml文件里面配置一下自己创建的实体类。
三、通知功能完整代码
MainActivity
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "leo";
public NotificationManager manager;
public Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("leo","测试通知",NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_ONE_SHOT);
}
notification = new NotificationCompat.Builder(this, "leo")
.setContentTitle("官方通知")
.setContentText("世界那么大,我想去看看")
.setSmallIcon(R.drawable.baseline_star_rate_24)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.baseline_star_rate_24))
.setColor(Color.parseColor("#ff0000"))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
}
public void sendNotification(View view){
manager.notify(1,notification);
}
public void cancelNotification(View view){
manager.cancel(1);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendNotification"
android:text="发出通知"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="cancelNotification"
android:text="取消通知"
>
</Button>
</LinearLayout>
NotificationActivity
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
public class NotificationActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("leo","onCreate:进步NotificationActivity");
}
}
运行成功: