使用Notification在状态栏上显示通知

运行效果图:

            

结构目录:

注意事项:

如果logcat有错误提示:No Channel found for pkg, 可参考链接:https://blog.csdn.net/u010356768/article/details/83546008

activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:background="@drawable/background"
 8     tools:context=".MainActivity">
 9
10
11 </RelativeLayout>

activity_detail.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:background="@drawable/img"
 8     tools:context=".DetailActivity">
 9
10 </RelativeLayout>
MainActivity:
 1 package com.mingrisoft.notification;
 2
 3 import android.app.Notification;
 4 import android.app.NotificationChannel;
 5 import android.app.NotificationManager;
 6 import android.app.PendingIntent;
 7 import android.content.Intent;
 8 import android.graphics.Color;
 9 import android.os.Build;
10 import android.support.annotation.RequiresApi;
11 import android.support.v4.app.NotificationCompat;
12 import android.support.v7.app.AppCompatActivity;
13 import android.os.Bundle;
14 import android.util.Log;
15
16 public class MainActivity extends AppCompatActivity {
17     final int NOTIFIYID = 0x123;    //通知的ID
18
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23
24         //获取通知管理器用于发送通知
25         final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
26         //创建一个Notification对象
27         Notification.Builder notification = new Notification.Builder(MainActivity.this);
28         //设置打开该通知,该通知自动消失
29         notification.setAutoCancel(true);
30         //设置通知的图标
31         notification.setSmallIcon(R.drawable.packet);
32         //设置通知的标题
33         notification.setContentTitle("奖励百万红包");
34         //设置通知的内容
35         notification.setContentText("点击查看详情");
36         //设置发送时间
37         notification.setWhen(System.currentTimeMillis());
38         //设置使用系统默认的声音、震动
39         notification.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
40         //兼容android8.0
41         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
42             NotificationChannel channel = new NotificationChannel("001","my_channel",NotificationManager.IMPORTANCE_DEFAULT);
43             channel.enableLights(true); //是否在桌面icon右上角展示小红点
44             channel.setLightColor(Color.GREEN); //小红点颜色
45             channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
46             notificationManager.createNotificationChannel(channel);
47             notification.setChannelId("001");
48         }
49
50         //创建一个启动DetailActivity的Intent
51         Intent intent = new Intent(MainActivity.this, DetailActivity.class);
52         PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
53         //设置通知栏点击跳转
54         notification.setContentIntent(pendingIntent);
55         //发送通知
56         notificationManager.notify(NOTIFIYID, notification.build());
57     }
58 }
DetailActivity:
 1 package com.mingrisoft.notification;
 2
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5
 6 public class DetailActivity extends AppCompatActivity {
 7
 8     @Override
 9     protected void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.activity_detail);
12     }
13 }

因为程序要访问系统震动器,所以要声明权限:

manifests:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.mingrisoft.notification">
 4
 5     <uses-permission android:name="android.permission.VIBRATE"></uses-permission>
 6
 7     <application
 8         android:allowBackup="true"
 9         android:icon="@mipmap/ic_launcher"
10         android:label="@string/app_name"
11         android:roundIcon="@mipmap/ic_launcher_round"
12         android:supportsRtl="true"
13         android:theme="@style/AppTheme">
14         <activity android:name=".MainActivity">
15             <intent-filter>
16                 <action android:name="android.intent.action.MAIN" />
17
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21         <activity android:name=".DetailActivity"></activity>
22     </application>
23
24 </manifest>

原文地址:https://www.cnblogs.com/hemeiwolong/p/12608946.html

时间: 2024-08-02 13:07:04

使用Notification在状态栏上显示通知的相关文章

Android中使用Notification在状态栏上显示通知

场景 状态栏上显示通知效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建NotificationActivity,通过getSystemService方法获取通知管理器. 然后创建通知并设置通知的一些属性,再使用通知管理器发送通知. package com.badao.relativelayouttest; import androidx.annotation.Req

Ionic app 通知在Moto 360 Watch上显示通知(2)

在前一篇文章中,我们已经将Wtach的环境测试成功,下面进入我们自己消息的接收. 1.安装JPush插件在我们的App中,这个具体步骤可以参考 Ionic 安装JPush过程 2.在App上的登录模块设置别名,因为我们的目标是,根据不同的账户发送不同的消息. .controller("LoginController",function ($scope, $state, AccountService, $rootScope, $stateParams, $ionicHistory, $i

在状态栏上显示时间

// TODO: 在此添加消息处理程序代码和/或调用默认值 //获得当前的系统时间 CTime t=CTime::GetCurrentTime();                         CClientDC dc(this); //格式可以参考MSDN中的strftime函数 CString str=_T("时间:")+ t.Format("%Y-%m-%d %H:%M:%S"); //设置一个CClientDC对象来获取str的长度      CSize

做一个计时器示例 在页面上有一个文本框和一个普通按钮 在状态栏上显示,单击按钮,开始计时的提示信息 单击按钮时,在文本框显示计数的过程,同时要求按钮上的字,显示为“单击暂停” 此时单击按钮,暂停计时,同时按钮上的字显示为“继续计时”,要求这两种状态来回切换,来完成计数器的功能

2015-04-12 20:15:25 方法一; <script> var s=0; var timeout=0; status='点击按钮,开始计时'; function count(){ s++; time1.value=s; timeout=setTimeout('count();',100); } function onChange(){ if(timeout==0){ count(); time2.value='暂停计时'; status='点击按钮,暂停计时'; }else{ cl

android:Notification实现状态栏的通知

在使用手机时,当有未接来电或者新短消息时,手机会给出响应的提示信息,这些提示信息一般会显示到手机屏幕的状态栏上. Android也提供了用于处理这些信息的类,它们是Notification和NotificationManager. 当中,Notification代表的是具有全局效果的通知,而NotificationManager则是用于发送Notification通知的系统服务. 使用Notification和NotificationManager类发送和显示通知也比較简单,大致能够分为下面四个

在状态栏上做渐变动画效果

效果 说明 1. 在状态栏上显示信息并覆盖状态栏信息需要技巧 2. 本设计支持外部消息view的定制,只需要实现规定的协议方法即可 源码 https://github.com/YouXianMing/StatusBarMessage // // StatusMessage.h // StatusBarView // // Created by YouXianMing on 15/7/24. // Copyright (c) 2015年 YouXianMing. All rights reserv

通知栏Notification在不同手机上显示的问题总结

可以参照http://blog.csdn.net/vipzjyno1/article/details/25248021,这里面关于通知的写的不错,也很全面,我的这篇主要是记录自己在适配上遇到的问题. 通知的统一的创建方式: NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext); 而通知的管理则是使用NotificationManager是用来管理通知的,使用如下:先初始化用到的系统服务,然后调

winform中的状态栏,以及在状态栏目上显示时间

1:在winform上添加状态栏,并且在状态栏目上多添加几个label. step1:拖一个StatusStrip到winform上,名字默认为statusStrip1.找到statusStrip1的items属性,双击打开添加3个StatusLabel.名字默认分别为toolStripStatusLabel1.toolStripStatusLabel2.toolStripStatusLabel3.他们分别是从左到右显示.设置toolStripStatusLabel2的属性的BorderSide

极光推送 状态栏图标显示不全(原创)

极光论坛很多人遇到这种问题:在收到推送消息的那一瞬间,推送图标被截取.在极光论坛大多数人的答案是图片适配,既在不同的drawble文件夹放推送的icon,然,极光推送默认调用应用的launcher_icon,我的手机分辨率是1920*1080,调用drawable-xhdpi文件夹里面的launcher_icon,大小为144*144,而在drawable-xhdpi中的通知的图标大小为48*48才会显示全,所以出现被截取的现象是很正常不过. 问题来了,既然极光默认调用应用的launcher_i