Android实现系统消息推送

所属分类: 软件编程 / Android 阅读数: 63
收藏 0 赞 0 分享

现在好多应用都接入了推送功能,市面上也有很多关于推送的第三方,例如极光等等,那么我们需求不大,接入极光会造成很大的资源浪费,下面我们来看下利用android服务进行本地推送消息。

1.注册一个Service

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import java.util.Calendar;
 
/**
 * Created by 70883 on 2017/8/10.
 */
public class PushSmsService extends Service {
 private NotificationManager manager;
 private PendingIntent pi;
 private MyThread myThread;
 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
   return null;
   }
 
 @Override
 public void onCreate() {
  myThread = new MyThread();
  myThread.start();
  super.onCreate();
 }
 
 @Override
 public void onDestroy() {
  super.onDestroy();
 }
   @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
   private void notification() {
    // 获取系统的通知管理器 
    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(getApplicationContext(),
      MainActivity.class);
    pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
    Notification notification = new Notification.Builder(getApplicationContext())
      .setAutoCancel(true)
      .setContentText("工作在忙,也要吃饭哦")
      .setContentIntent(pi)
      .setSmallIcon(R.mipmap.ic_icon)
      .setWhen(System.currentTimeMillis())
      .build();
    notification.defaults = Notification.DEFAULT_ALL; // 使用默认设置,比如铃声、震动、闪灯 
     notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失 
    notification.flags |= Notification.FLAG_NO_CLEAR;// 点击通知栏的删除,消息不会依然不会被删除 
    manager.notify(0, notification);
   }
 private class MyThread extends Thread{
  private Calendar c ;
  @Override
  public void run() {
   while (true){
    c = Calendar.getInstance();
    if(c.get(Calendar.HOUR_OF_DAY) == 15){
      try {
       notification();
       sleep(1000*60*60);
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
    }
   }
 
  }
 }
}

2.在AndroidMan中注册

<service android:name=".ui.Service.PushSmsService"></service>

3.由于我是需要全局应用就在Application中进行启动了 

public void startService() {
  Intent intent = new Intent(this, PushSmsService.class);
  // 启动服务
  startService(intent);
 }

4.也可以配合服务端使用,定时推送消息

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android 自定义球型水波纹带圆弧进度效果(实例代码)

最近小编接到一个这样的需求,需要实现一个圆形水波纹,带进度,两层水波纹需要渐变显示,且外围有一个圆弧进度。今天小编给大家分享实例代码,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Flutter 实现下拉刷新上拉加载的示例代码

这篇文章主要介绍了Flutter 实现下拉刷新上拉加载的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Windows实现Flutter环境搭建及配置这一篇就够了

这篇文章主要介绍了Windows实现Flutter环境搭建及配置这一篇就够了,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android利用碎片fragment实现底部标题栏(Github模板开源)

Fragment可以作为Activity的组成部分,一个Activity可以有多个Fragment,这篇文章主要介绍了Android利用碎片fragment实现底部标题栏(Github模板开源),需要的朋友可以参考下
收藏 0 赞 0 分享

android studio 的下拉菜单Spinner使用详解

这篇文章主要介绍了android studio 的下拉菜单Spinner使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解析Android 8.1平台SystemUI 导航栏加载流程

这篇文章主要介绍了Android 8.1平台SystemUI 导航栏加载流程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信录音功能

这篇文章主要为大家详细介绍了Android仿微信录音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信键盘切换效果

这篇文章主要为大家详细介绍了Android仿微信键盘切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android超清晰6.0权限申请AndPermission

这篇文章主要介绍了Android超清晰6.0权限申请AndPermission,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信录制语音功能

这篇文章主要介绍了Android仿微信录制语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多