栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android 基础开发组件Notification

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 基础开发组件Notification

1.获取NotificationManager对象
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2. 安卓8.0以上版本的通知需要创建NotificationChannel
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
  NotificationChannel channel = new NotificationChannel("june","通知",NotificationManager.IMPORTANCE_HIGH);
  manager.createNotificationChannel(channel);
}
3.使用Builder构造器创建Notification对象,通知设置 setContentTitle

设置通知标题

setContentText

设置通知内容

setSmallIcon

设置通知小图标

setLargeIcon

设置通知大图标

setColor

设置小图标颜色

setContentIntent

设置点击后跳转意图

setAutoCancel

点击通知后自动清除通知

notification = new NotificationCompat.Builder(this,"june")
  .setContentTitle("官方通知")
  .setContentText("相由心生,道法自然")
  .setSmallIcon(R.drawable.ic_android_black_24dp)
  .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.user))
  .setColor(Color.parseColor("#ff0000"))
  .setContentIntent(pendingIntent)
  .setAutoCancel(true)
  .build();
4.设置跳转意图
Intent intent = new Intent(this, NotificationActivity.class);
@SuppressLint("UnspecifiedImmutableFlag") PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

NotificationActivity.java

package com.weijun901.app;

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("june", "onCreate: 进入NotificationActivity");
  }
}

notification.xml




  
    

Notification.java

package com.weijun901.app;

import android.annotation.SuppressLint;
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.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;

public class Notification extends AppCompatActivity {

  private NotificationManager manager;
  private android.app.Notification notification;

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

    //1.获取NotificationManager对象
    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    //2.安卓8.0以上版本的通知需要创建NotificationChannel
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
      NotificationChannel channel = new NotificationChannel("june","通知",NotificationManager.IMPORTANCE_HIGH);
      manager.createNotificationChannel(channel);
    }

    //4.设置跳转意图
    Intent intent = new Intent(this, NotificationActivity.class);
    @SuppressLint("UnspecifiedImmutableFlag") PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    //3.使用Builder构造器创建Notification对象,通知设置
    notification = new NotificationCompat.Builder(this,"june")
      .setContentTitle("官方通知")
      .setContentText("相由心生,道法自然")
      .setSmallIcon(R.drawable.ic_android_black_24dp)
      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.user))
      .setColor(Color.parseColor("#ff0000"))
      .setContentIntent(pendingIntent)
      .setAutoCancel(true)
      .build();
  }

  //5.发送通知
  public void send(View view) {
    manager.notify(1,notification);
  }

  //6.取消通知
  public void cancel(View view) {
    manager.cancel(1);
  }
}
5.发送通知
public void send(View view) {
  manager.notify(1,notification);
}
6.取消通知
public void send(View view) {
  manager.notify(1,notification);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953002.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号