为了账号安全,请及时绑定邮箱和手机立即绑定

腾讯信鸽自定义推送通知

标签:
Android

使用信鸽的过程,感觉一路艰辛,各种坑,想必各位使用过的也是深有体会的吧。而且官方文档也太简洁了。demo功能也不全,没办法只能自己摸索着来,这不刚把自定义通知弄明白,就给各位看官献上来了。

1. XGPushManager功能类

自定义本地通知样式 
void setPushNotificationBuilder(Context context, int notificationBulderId, XGPushNotificationBuilder notificationBuilder) 
本地通知,调用下面这个方法,就可以起来一个推送通知 
long addLocalNotification(Context context, XGLocalMessage msg)

2 如何自定义通知

这里主要就是需要构造一个XGPushNotificationBuilder

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

XGCustomPushNotificationBuilder build = new XGCustomPushNotificationBuilder();

    build.setSound(

            RingtoneManager.getActualDefaultRingtoneUri(

                    context,   RingtoneManager.TYPE_ALARM)) // 设置声音

                    //   setSound(

                    //   Uri.parse("android.resource://" + getPackageName()

                    //   + "/" + R.raw.wind)) 设定Raw下指定声音文件

                    .setDefaults(Notification.DEFAULT_VIBRATE)   // 振动

                    .setFlags(Notification.FLAG_NO_CLEAR);   // 是否可清除

    // 设置自定义通知layout,通知背景等可以在layout里设置

    build.setLayoutId(R.layout.layout_notification);

    // 设置自定义通知内容id

    build.setLayoutTextId(R.id.ssid);

    // 设置自定义通知标题id

    build.setLayoutTitleId(R.id.title);

    // 设置自定义通知图片id

    build.setLayoutIconId(R.id.icon);

    // 设置自定义通知图片资源

    build.setLayoutIconDrawableId(R.drawable.ic_launcher);

    // 设置状态栏的通知小图标

    build.setIcon(R.drawable.ic_launcher);

    // 设置时间id

    build.setLayoutTimeId(R.id.time);

    // 若不设定以上自定义layout,又想简单指定通知栏图片资源

    build.setNotificationLargeIcon(R.drawable.tenda_icon);

 

3如何使用我们自定义的通知

这个是替换默认的通知,build是上面的那段代码的,这样通知就是使用我们自定义的形式了。
XGPushManager.setDefaultNotificationBuilder(context, build);

4 启动本地通知

[代码]java代码:

?

1

2

XGLocalMessage  localMessage = new XGLocalMessage();

XGPushManager.addLocalNotification(context,   localMessage);

 

5如何根据推送信息的不同,来显示不同的推送通知式样

最初我总是入不了这个门,原因是没有理解到推送消息和推送通知的区别,如果我们要想使用自定义通知来显示,我们就需要使用推送消息,信鸽就只是将推送的内容传递过来,它说这是透传,然后用户根据这些消息,自己做自己想做的事情。 
XGPushBaseReceiver类提供透传消息的接收和操作结果的反馈,需要开发者继承本类,并重载相关的方法; 
void onTextMessage(Context context,XGPushTextMessage message)该方法就是接收透传消息的方法。这样我们需要写一个receiver来继承XGPushBaseReceiver,直接上代码了

[代码]java代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

public class MessageReceiver   extends XGPushBaseReceiver   {

    private Intent intent = new    Intent("com.qq.xgdemo.activity.UPDATE_LISTVIEW");

 public static final String   LogTag = "TPushReceiver";

 

private void show(Context   context, String text) {

    Toast.makeText(context,   text, Toast.LENGTH_SHORT).show();

}

 

// 通知展示

@Override

public void onNotifactionShowedResult(Context   context,

        XGPushShowedResult   notifiShowedRlt) {

           if (context == null || notifiShowedRlt == null) {

                return;

           }

 

    show(context, "您有1条新消息,   " + "通知被展示 , " + notifiShowedRlt.toString());

}

 

@Override

public void onUnregisterResult(Context   context, int errorCode)   {

    if (context == null) {

        return;

    }

    String text = "";

    if (errorCode == XGPushBaseReceiver.SUCCESS)   {

        text   = "反注册成功";

    } else {

        text   = "反注册失败" + errorCode;

    }

    Log.d(LogTag,   text);

    show(context,   text);

 

}

 

@Override

public void onSetTagResult(Context   context, int errorCode,   String tagName) {

    if (context == null) {

        return;

    }

    String text = "";

    if (errorCode == XGPushBaseReceiver.SUCCESS)   {

        text   = "\"" + tagName + "\"设置成功";

    } else {

        text   = "\"" + tagName + "\"设置失败,错误码:" + errorCode;

    }

    Log.d(LogTag,   text);

    show(context,   text);

 

}

 

@Override

public void onDeleteTagResult(Context   context, int errorCode,   String tagName) {

    if (context == null) {

        return;

    }

    String text = "";

    if (errorCode == XGPushBaseReceiver.SUCCESS)   {

        text   = "\"" + tagName + "\"删除成功";

    } else {

        text   = "\"" + tagName + "\"删除失败,错误码:" + errorCode;

    }

    Log.d(LogTag,   text);

    show(context,   text);

 

}

 

// 通知点击回调 actionType=1为该消息被清除,actionType=0为该消息被点击

@Override

public void onNotifactionClickedResult(Context   context,

        XGPushClickedResult   message) {

    if (context == null || message == null) {

        return;

    }

    String text = "";

    sendIconCountMessage(context);

    samsungShortCut(context,   "25");

    if (message.getActionType() ==   XGPushClickedResult.NOTIFACTION_CLICKED_TYPE) {

        //   通知在通知栏被点击啦。。。。。

        //   APP自己处理点击的相关动作

        //   这个动作可以在activity的onResume也能监听,请看第3点相关内容

        text   = "通知被打开 :" + message;

    } else if (message.getActionType() ==   XGPushClickedResult.NOTIFACTION_DELETED_TYPE) {

        //   通知被清除啦。。。。

        //   APP自己处理通知被清除后的相关动作

        text   = "通知被清除 :" + message;

    }

    Toast.makeText(context,   "广播接收到通知被点击:" + message.toString(),

            Toast.LENGTH_SHORT).show();

    // 获取自定义key-value

    String   customContent = message.getCustomContent();

    if (customContent != null && customContent.length() != 0) {

        try {

            JSONObject   obj = new JSONObject(customContent);

            //   key1为前台配置的key

            if (!obj.isNull("ID")) {

                String   value = obj.getString("ID");

                Log.d(LogTag,   "get custom value:" + value);

            }

            //   ...

        }   catch (JSONException   e) {

            e.printStackTrace();

        }

    }

    // APP自主处理的过程。。。

    Log.d(LogTag,   text);

    show(context,   text);

}

 

@Override

public void onRegisterResult(Context   context, int errorCode,

        XGPushRegisterResult   message) {

    // TODO   Auto-generated method stub

    if (context == null || message == null) {

        return;

    }

    String text = "";

    if (errorCode == XGPushBaseReceiver.SUCCESS)   {

        text   = message + "注册成功";

        //   在这里拿token

        String   token = message.getToken();

    } else {

        text   = message + "注册失败,错误码:" + errorCode;

    }

    Log.d(LogTag, text);

    show(context,   text);

}

 

// 消息透传

@Override

public void onTextMessage(Context   context, XGPushTextMessage message) {

    // TODO   Auto-generated method stub

    show(context, "haha");

    String text = "收到消息:" + message.toString();

    // 获取自定义key-value

    String   customContent = message.getCustomContent();

    if (customContent != null && customContent.length() != 0) {

        try {

            JSONObject   obj = new JSONObject(customContent);

            //   key1为前台配置的key

            if (!obj.isNull("key")) {

                String   value = obj.getString("key");

                Log.d(LogTag,   "get custom value:" + value);

            }

            //   ...

        }   catch (JSONException   e) {

            e.printStackTrace();

        }

    }

    // APP自主处理消息的过程...

    XGLocalMessage    localMessage = new XGLocalMessage();

    localMessage.setTitle("haha");

    localMessage.setContent(message.getContent());

    XGCustomPushNotificationBuilder   build = new XGCustomPushNotificationBuilder();

    build.setSound(

            RingtoneManager.getActualDefaultRingtoneUri(

                    context,   RingtoneManager.TYPE_ALARM)) // 设置声音

                    //   setSound(

                    //   Uri.parse("android.resource://" + getPackageName()

                    //   + "/" + R.raw.wind)) 设定Raw下指定声音文件

                    .setDefaults(Notification.DEFAULT_VIBRATE)   // 振动

                    .setFlags(Notification.FLAG_NO_CLEAR);   // 是否可清除

    // 设置自定义通知layout,通知背景等可以在layout里设置

    build.setLayoutId(R.layout.layout_notification);

    // 设置自定义通知内容id

    build.setLayoutTextId(R.id.ssid);

    // 设置自定义通知标题id

    build.setLayoutTitleId(R.id.title);

    // 设置自定义通知图片id

    build.setLayoutIconId(R.id.icon);

    // 设置自定义通知图片资源

    build.setLayoutIconDrawableId(R.drawable.ic_launcher);

    // 设置状态栏的通知小图标

    build.setIcon(R.drawable.ic_launcher);

    // 设置时间id

    build.setLayoutTimeId(R.id.time);

    // 若不设定以上自定义layout,又想简单指定通知栏图片资源

    build.setNotificationLargeIcon(R.drawable.tenda_icon);

    // 客户端保存build_id

    XGPushManager.setDefaultNotificationBuilder(context,   build);

 

    XGPushManager.addLocalNotification(context,   localMessage);

    Log.d(LogTag,   text);

    show(context,   text);

}

private void sendIconCountMessage(Context   context)  {

    Intent it = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");

    it.putExtra("android.intent.extra.update_application_component_name",   "com.example.wujie.xungetest/.MainActivity");

    String iconCount   = "50";

    it.putExtra("android.intent.extra.update_application_message_text",   iconCount);

    context.sendBroadcast(it);

}

}

 

这样就可以完美自定义了,觉得有用,就请顶一下,谢谢

原文链接:http://www.apkbus.com/blog-682543-61187.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消