·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> Android开发 >> Android顶栏定时推送消息

Android顶栏定时推送消息

作者:佚名      Android开发编辑:admin      更新时间:2022-07-23

在用安卓设备时,经常会应用到弹出推送消息。下面在此把我之前写的推送代码分享给大家,供大家参考,有不同见解的朋友欢迎提出,共同学习进步!

最近搜索看这个的朋友比较多。这个也只是单独的内置推送。时时推送与服务器关联 我们可以用SDK云推送来实现我们所需的需求。相关介绍内容。往下移!

首先XML

<!-- 安卓推送服务 -->
<service android:name=".MessageService"
android:enabled="true"
android:exported="true"
android:label="PushService"
android:launchMode="singleInstance"
android:persistent="true"
android:process=":push" >
<intent-filter>
<action android:name="com.xxxx.action.MY_SERVICE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
<receiver
android:name="com.tt.xxxxx.download.GTAlarmReceiver"
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>
</receiver>
<!--end add-->

里面的. :。这些符号很坑爹。不懂的可以查下字段属性说明。

推送类:

/******************************** 类 *************************************/
package com.ttad.yxcb;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import com.tt.yingxiongchibis.download.GTDownloaderActivity;
public class MessageService extends Service {
//获取消息线程
private MessageThread messageThread = null;
//点击查看
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null;
//通知栏消息
private int messageNotificationID = 1000;
private Notification messageNotification = null;
private NotificationManager messageNotificatioManager = null;
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
//初始化
messageNotification = new Notification();
messageNotification.icon = R.drawable.app_icon_ucs;
messageNotification.tickerText = "알림";
messageNotification.defaults = Notification.DEFAULT_SOUND;
messageNotificatioManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//点击跳转的activity
messageIntent = new Intent(this, GTDownloaderActivity.class);
messagePendingIntent = PendingIntent.getActivity(this,0,messageIntent,0);
//开启线程
messageThread = new MessageThread();
messageThread.isRunning = true;
messageThread.start();
//Toast.makeText(MessageService.this, "", Toast.LENGTH_LONG).show();
super.onCreate();
}
/**
* 从服务器端获取消息
*
*/
class MessageThread extends Thread{
//运行状态,下一步骤有大用
public boolean isRunning = true;
public void run() {
while(isRunning){
try {
//休息10分钟
Thread.sleep(1000);
//获取服务器消息
String serverMessage = getServerMessage();
if(serverMessage!=null&&!"".equals(serverMessage)){
//更新通知栏
messageNotification.setLatestEventInfo(MessageService.this,"알림",serverMessage,messagePendingIntent);
messageNotificatioManager.notify(messageNotificationID, messageNotification);
//每次通知完,通知ID递增一下,避免消息覆盖掉
messageNotificationID++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void onDestroy() {
// System.exit(0);
//或者,二选一,推荐使用System.exit(0),这样进程退出的更干净
messageThread.isRunning = false;
//super.onDestroy();
}
/**
* 这里以此方法为服务器Demo,仅作示例
* @return 返回服务器要推送的消息,否则如果为空的话,不推送
*/
public String getServerMessage(){
SimpleDateFormat sdf=new SimpleDateFormat("HHmmss");
String date=sdf.format(new java.util.Date());
String in = date;
if(date.equals("195500"))
{
String str = "잠시후 전쟁터 시작됩니다. 준비해주세요.";
return str;
}
else if(date.equals("212500"))
{
String str = "잠시후 보스전 시작됩니다. 준비해주세요.";
return str;
}
else
{
return "";
}
}
}

最后,调用方式:

//推送
Intent intent = new Intent();
// 设置Action属性
intent.setAction("com.ttad.yxcb.action.MY_SERVICE");
// 启动该Service
startService(intent);
// startService(new Intent(ExTextActivity.this, MessageService.class));

Android消息推送知识补充:

1.引言

  所谓的消息推送就是从服务器端向移动终端发送连接,传输一定的信息。比如一些新闻客户端,每隔一段时间收到一条或者多条通知,这就是从服务器端传来的推送消息;还比如常用的一些IM软件如微信、GTalk等,都具有服务器推送功能。

  推送方法如下:

  1)通过SMS进行服务器端和客户端的交流通信。

  在Android平台上,你可以通过拦截SMS消息并且解析消息内容来了解服务器的意图,可以实现完全的实时操作。但是问题是这个方案的成本相对比较高,且依赖于运营商。

  2)循环主动定时获取

  这种方法需要客户端来做一个定时或者周期性的访问服务器端接口,以获得最新的消息。轮询的频率太慢可能导致某些消息的延迟,太快则会大量消耗网络带宽和电池。

  3)持久连接

  这个方案可以解决由轮询带来的性能问题,但是还是会消耗手机的电池。我们需要开一个服务来保持和服务器端的持久连接(苹果就和谷歌的C2DM是这种机制)。但是对于Android系统,当系统可用资源较低,系统会强制关闭我们的服务或者是应用,这种情况下连接会强制中断。(Apple的推送服务之所以工作的很好,是因为每一台手机仅仅保持一个与服务器之间的连接,事实上C2DM也是这么工作的。即所有的推送服务都是经由一个代理服务器完成的,这种情况下只需要和一台服务器保持持久连接即可。C2DM=Cloud to Device Messaging)。

  相比之下第三种还是最可行的。为软件编写系统服务或开机启动功能;或者如果系统资源较低,服务被关闭后可以在onDestroy ()方法里面再重启该服务,进而实现持久连接的方式。

  C2DM内置于Android的2.2系统上,无法兼容老的1.6到2.1系统;且依赖于Google官方提供的C2DM服务器,由于国内的网络环境,这个服务经常不可用。

  建立在TCP协议之上的XMPP协议,不仅可提供可这种持久连接的功能,能实现服务器和客户机的双工通信,还能不依赖与系统版本和google服务器的限制,提供了比较好的解决方案。

2. XMPP协议

  XMPP全称Extensible Messaging and Presence Protocol,前身是Jabber项目,是一种以XML为基础的开放式即时通讯协议。XMPP因为被Google Talk和网易泡泡应用而被广大网民所接触。XMPP的关键特色是,分散式的即时通讯系统,以及使用XML串流。XMPP目前被IETF国际标准组织完成了标准化工作。

  Android push notification(androidpn) 是一个基于XMPP协议的java开源实现,它包含了完整的客户端和服务器端。该服务器端基本是在另外一个开源工程openfire基础上修改实现的。

  androidpn客户端需要用到一个基于java的开源XMPP协议包asmack,这个包同样也是基于openfire下的另外一个开源项目smack,不过我们不需要自己编译,可以直接把androidpn客户端里面的asmack.jar拿来使用。客户端利用asmack中提供的XMPPConnection类与服务器建立持久连接,并通过该连接进行用户注册和登录认证,同样也是通过这条连接,接收服务器发送的通知。

  androidpn服务器端也是java语言实现的,基于openfire开源工程,不过它的Web部分采用的是spring框架,这一点与openfire是不同的。Androidpn服务器包含两个部分,一个是侦听在5222端口上的XMPP服务,负责与客户端的XMPPConnection类进行通信,作用是用户注册和身份认证,并发送推送通知消息。另外一部分是Web服务器,采用一个轻量级的HTTP服务器,负责接收用户的Web请求。服务器的这两方式,意义非凡:当相应的TCP端口被防火墙封闭,可以使用轮询的方式进行访问,因此又有助于通过防火墙。