Notification通知的使用实例

什么是通知:

当某个程序希望向用户发送一些提示信息时,而该应用又不在前台运行,这时借助通知就可以轻松实现,当触发通知时,状态栏会显示一个小图标,下拉状态栏就可以看到通知的详细信息了。

实例讲解:
效果展现:

代码实现:(需要说明的是这个通知在Android8.0无法运行,我以后有时间会补充,我是在5.0中运行的)
1.修改activity_main.xml中的代码,添加按钮:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.notificationtest.MainActivity">

    <Button
        android:id="@+id/send_notification_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="send notification" />

</LinearLayout>
2.修改MainActivity中的代码,实现发送通知:
public class MainActivity extends AppCompatActivity {

    private Button notifyBtn;


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

        notifyBtn = findViewById(R.id.send_notification_btn);
        //对发送通知的按钮进行监听
        notifyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建NotificationManager对象
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                //通过NotificationCompat.Builder()方法获取Notification对象
                Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        .setContentTitle("hello world")
                        .setContentText("i miss you ,you know?")
                        .setWhen(System.currentTimeMillis())
                        //设置小图标,就是点击通知按钮后,状态栏上的小白点
                        .setSmallIcon(R.mipmap.ic_launcher)
                        //下拉状态栏后,看到的图标
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .build();
                //最后发布通知
                /**
                 * Post a notification to be shown in the status bar. If a notification with
                 * the same id has already been posted by your application and has not yet been canceled, it
                 * will be replaced by the updated information.
                 *
                 * @param id An identifier for this notification unique within your
                 *        application.    设置ID,试你的通知唯一
                 * @param notification A {@link Notification} object describing what to show the user. Must not
                 *        be null.    第二个参数就是Notification对象
                 */
                manager.notify(1,notification);
            }
        });
    }
}
over
但是,当我们点击这条通知时,并没用任何反应,这和现实中不一样,下面我们就来实现点击通知调转到相应的页面。
1.创建点击通知跳转的页面,然后需修改布局,让你能看出实现调转了:
<?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="com.example.notificationtest.NotificationActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is notification"
        android:layout_gravity="center_horizontal"
        android:textSize="25sp"/>

</LinearLayout>
2.修改MainActivity中不分代码:
........
     notifyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建intent对象,表名intent的意图
                Intent intent = new Intent(MainActivity.this,NotificationActivity.class);
                //创建PendingIntent对象,param1:当前环境,param2:通常0就行,param3:pendingIntent意图,param4:pendingIntent意图,一般填入0就行
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
                //创建NotificationManager对象
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                //通过NotificationCompat.Builder()方法获取Notification对象
                Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        ............
                        .setWhen(System.currentTimeMillis())
                        //点击通知后自动取消
                        .setAutoCancel(true)
                        //实现跳转
                        .setContentIntent(pendingIntent)
                        .........
                        .build();
                //最后发布通知
               ...........
            }
        });
    }
}
效果展现:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

intoSunshine

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值