cocos2d-js 安卓自定义本地通知功能
安卓新手,笔记有理解不当的地方望指出,经过几天折腾终于可以实现类似ios的本地通知功能(ios就几行代码),可能有第三方sdk可以方便实现,暂时没去找
思路:
1。 startService 和bindService 同时存在
startService 新开进程 注意AndroidMenifest.xml中添加process
<service android:name="org.cocos2dx.javascript.LocalPush"
android:process=":remote">
</service>
这里的remote可以自定义任意名称,前面冒号代表与原Activity不同的独立进程
因为两个进程互相独立,需要通讯,使用AIDL方式
Activity.java
@Override
protected void onStart() {
Log.i(TAG,"onStart");
super.onStart();
Bundle args = new Bundle();
args.putString("param", "oper1");
Intent intent = new Intent(AppActivity.this, LocalPush.class);
intent.putExtras(args);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
Log.i(TAG,"onStop");
super.onStop();
unbindService(mConnection);
}
static forService mService;
private forActivity mCallback = new forActivity.Stub() {
@SuppressLint("ShowToast") public void performAction() throws RemoteException {
Toast.makeText(AppActivity.this, "this toast is called from service", ).show();
}
};
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.i(TAG,"onServiceConnected");
mService = forService.Stub.asInterface(service);
try {
mService.registerTestCall(mCallback);
}
catch (RemoteException e) {
}
}
public void onServiceDisconnected(ComponentName className) {
Log.i(TAG,"disconnect service");
mService = null;
}
};
LocalPush.java 继承自 Service
public class LocalPush extends Service {
private forActivity callback; // public LocalPush() {
// super("LocalPush");
// }
//
// @Override
// protected void onHandleIntent(Intent intent) {
// String action = intent.getExtras().getString("param");
// Log.i(TAG, "onHandleIntent");
// notifyArr = new Vector<CustomNotify>();
// messageNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// // IntentService会使用单独的线程来执行该方法的代码
// while (isRunning) {
// Log.i(TAG, "Service tick--->1");
// try {
// Log.i(TAG, "Service tick--->2");
// //休息1秒
// Thread.sleep(1000);
// Log.i(TAG, "Service tick--->3");
// sendNotifies();
// Log.i(TAG, "Service tick--->4");
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
// private final IBinder binder = new LocalBinder();
// public class LocalBinder extends Binder {
// LocalPush getService() {
// return LocalPush.this;
// }
// }
private final forService.Stub mBinder = new forService.Stub() {
@Override
public void invokCallBack(int _alertTime, String _key, String _alertBody, String _title, String _content, boolean _isEndClean) throws RemoteException {
//callback.performAction();
registerLocalNotification(_alertTime, _key, _alertBody, _title, _content, _isEndClean);
}
@Override
public void registerTestCall(forActivity cb) throws RemoteException {
callback = cb;
}
};
//当使用startService()方法启动Service时
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "IBinder onBin");
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG,"service on unbind");
return super.onUnbind(intent);
}
public void onRebind(Intent intent) {
Log.i(TAG, "service on rebind");
super.onRebind(intent);
} //Service时被调用
@SuppressWarnings("deprecation")
@Override
public void onCreate() {
Log.i(TAG, "Service onCreate--->1");
super.onCreate();
//startForeground(-1213, new Notification());
Notification notification = new Notification();
notification.icon = R.drawable.icon; //通知图片
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(LocalPush.this, AppActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(LocalPush.this, , intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(LocalPush.this, "Slots", "Click return to Slots!", pendingIntent);
startForeground(0x1982, notification); // notification ID: 0x1982, you can name it as you will.
messageThread = new MessageThread();
messageThread.start();
}
class MessageThread extends Thread {
//运行状态
public boolean isRunning = true;
@Override
public void run() {
while(isRunning) {
synchronized(this) {
try {
//休息1秒
Thread.sleep();
sendNotifies(); //这里判断是否需要发出通知
} catch (Exception e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
}
}
@Override
//当调用者使用startService()方法启动Service时,该方法被调用
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service onStart--->1:"+ flags+" stargId:" + startId);
return super.onStartCommand(intent, flags, startId);
}
@Override
//当Service不在使用时调用
public void onDestroy() {
Log.i(TAG, "Service onDestroy--->");
super.onDestroy();
stopForeground(true);
}
}
forActivity.aidl
package org.cocos2dx.javascript;
interface forActivity {
void performAction();
}
forService.aidl
package org.cocos2dx.javascript;
import org.cocos2dx.javascript.forActivity;
interface forService {
void registerTestCall(forActivity cb);
void invokCallBack(int _alertTime, String _key, String _alertBody, String _title, String _content, boolean _isEndClean);
}
2 通知
cocos3 以上支持js反射到java,Activity中创建对应Static 方法 ,
大概这样
public static void registerLocalNotification(int _alertTime, String _key, String _alertBody, String _title, String _content, boolean _isEndClean) {
Log.i(TAG, "registerLocalNotification1 "+_alertTime+" "+_key+" "+_alertBody+" "+_title+" "+_content+" "+_isEndClean);
//mService.registerLocalNotification(_alertTime, _key, _alertBody, _title, _content, _isEndClean);
Log.i(TAG, "registerLocalNotification2 "+_alertTime+" "+_key+" "+_alertBody+" "+_title+" "+_content+" "+_isEndClean);
try {
mService.invokCallBack(_alertTime, _key, _alertBody, _title, _content, _isEndClean);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
service端相应方法,思路:开启新线程实时 比对通知世间簇和当前世间簇,如果结果为 小于等于 则发出通知
//注册通知
public void registerLocalNotification(int _alertTime, String _key, String _alertBody, String _title, String _content, boolean _isEndClean) {
...
}
发通知大概这样
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null;
//通知栏消息
private int messageNotificationID = 1000;
private Notification messageNotification = null;
private NotificationManager messageNotificationManager;
@SuppressWarnings("deprecation")
public void commitNotify () {
finishFlag = true;
//初始化
messageNotification = new Notification();
messageNotification.icon = R.drawable.icon; //通知图片
messageNotification.defaults = Notification.DEFAULT_SOUND;
//点击查看
messageIntent = new Intent(Intent.ACTION_MAIN);
messageIntent.addCategory(Intent.CATEGORY_LAUNCHER);
messageIntent.setClass(LocalPush.this, AppActivity.class);
messageIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); // //Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
//messageIntent = new Intent(LocalPush.this, AppActivity.class);
messagePendingIntent = PendingIntent.getActivity(LocalPush.this, , messageIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//设置消息内容和标题
messageNotification.setLatestEventInfo(LocalPush.this, title, content, messagePendingIntent);
//messageNotification.when = System.currentTimeMillis();
messageNotification.number = ;
//messageNotification.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
// 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示 //避免覆盖消息,采取ID自增
messageNotificationManager.notify(++ messageNotificationID, messageNotification);
}
cocos2d-js 安卓自定义本地通知功能的更多相关文章
- cocos2d JS 利用定时器实现-倒计时功能
//创建一个定时器 cc.director.getScheduler().schedule(this, this.updates, 1, cc.REPEAT_FOREVER, 0, false, &q ...
- iOS: 本地通知的前后变化(iOS10)
一.介绍 通知和推送是应用程序中很重要的组成部分.本地通知可以为应用程序注册一些定时任务,例如闹钟.定时提醒等.远程推送则更强大,提供了一种通过服务端主动推送消息到客户端的方式,服务端可以更加灵活地 ...
- UILocalNotification本地通知的使用方法
本文所写方法主要应用UILocalNotification达到本地推送通知栏信息 取消了其他教程里过期的UIAlertView方法 使用UILocalNotification主要分为创建 调用 取消 ...
- 利用JS实现自定义滚动条
一般默认的滚动条会比较丑,我们可以用简单的js实现自定义滚动条的功能: 代码如下: <!doctype html> <html> <head> <meta c ...
- 在Unity3D中实现安卓平台的本地通知推送
[前言] 对于手游来说,什么时候需要推送呢?玩过一些带体力限制的游戏就会发现,我的体力在恢复满后,手机会收到一个通知告诉我体力已完全恢复了.这类通知通常是由本地的客户端发起的,没有经过服务端. 在安卓 ...
- Ios开发中UILocalNotification实现本地通知实现提醒功能
这两天在做一个日程提醒功能,用到了本地通知的功能,记录相关知识如下: 1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time ...
- Zabbix的通知功能以及自定义脚本告警
本节内容: Zabbix的通知功能 定义接收告警的用户 定义Action Zabbix自定义脚本发送报警邮件 一.Zabbix的通知功能 在配置好监控项和触发器之后,一旦正常工作中的某触发器状态发生改 ...
- iOS开发中UILocalNotification本地通知实现简单的提醒功能
这段时间项目要求做一个类似的闹钟提醒功能,对通知不太熟悉的我,决定先用到xcode自带的本地通知试试,最终成功的实现了功能,特整理分享下. 它的表现特点: app关闭的时候也能接收和显示通知. app ...
- php框架tp3.2.3和js写的微信分享功能心得,分享的标题内容图片自定义
https://blog.csdn.net/weixin_42231483/article/details/81585322 最近用PHP的tp3.2.3框架和js写的微信分享功能心得,分享的标题内容 ...
随机推荐
- python气象分析
数据分析实例 -- 气象数据 一.实验介绍 本实验将对意大利北部沿海地区的气象数据进行分析与可视化.我们在实验过程中先会运用 Python 中matplotlib库的对数据进行图表化处理,然后调用 s ...
- UCenter 的目录结构
以下关于文件的阐述以及代码的样例,均以 PHP 程序为例. UCenter 的目录结构 UCenter 分为服务端和客户端 2 个部分.服务端目录为“upload/”,客户端目录为“client/ ...
- MacBook Pro 一月使用体验
从 2013 年开始,就特别想买 MBP,终于在 2015 年的尾巴用上了 MBPR.原本是要在使用一周后写一份使用体验的,但因为懒,现在拖到一个月了,刚好现在也是2016年的一月,就把标题改成一月使 ...
- jdk配置(备份)
#####set java environment #export JAVA_HOME=/usr/java/jdk1..0_172 #export JRE_HOME=${JAVA_HOME}/jre ...
- leetcode231
public class Solution { public bool IsPowerOfTwo(int n) { )) == && n > ); } } https://lee ...
- [Ora]-1309. OCI is not properly installed on this machine (NOE1/INIT)
When the Oracle client software has not been properly installed, you will get an exception when tryi ...
- 程序员必看:给你一份详细的Spring Boot知识清单
在过去两三年的Spring生态圈,最让人兴奋的莫过于Spring Boot框架.或许从命名上就能看出这个框架的设计初衷:快速的启动Spring应用.因而Spring Boot应用本质上就是一个基于Sp ...
- Spring MVC 异常处理 - DefaultHandlerExceptionResolver
对一些特殊的异常进行处理,比如方法类型不匹配, 转换错误.
- 傅里叶变换--MP3、JPEG和Siri背后的数学
http://blog.jobbole.com/51301/ 九年前,当我还坐在学校的物理数学课的课堂里时,我的老师为我们讲授了一种新方法,给我留下了深刻映像. 我认为,毫不夸张地说,这是对数学理论发 ...
- mysql启动报错 The server quit without updating PID file
[root@uz6542 data]# /etc/init.d/mysqld startStarting MySQL... ERROR! The server quit without updatin ...