起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3476|回复: 7

[分享] IOS安卓,设置通知声音消息等

[复制链接]

94

主题

403

帖子

683

积分

高级会员

Rank: 4

积分
683
QQ
发表于 2016-9-29 20:44:48 | 显示全部楼层 |阅读模式
IOS安卓,设置通知声音消息等, 采用的是别名。

package jpush;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.common.connection.HttpProxy;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.schedule.ScheduleResult;

import com.alibaba.fastjson.JSONObject;
import com.justep.baas.action.ActionContext;

public class Push {

    private static String appKey = "";
    private static String masterSecret = "";
    private static Boolean apnsProduction = false;
    private static JPushClient jpushClient;
    private static Boolean useProxy = false;

    static{
        InputStream configFile = Push.class.getResourceAsStream("jpush.config.xml");
        try{
            SAXReader reader = new SAXReader();
            Document doc = reader.read(configFile);
            Element config = doc.getRootElement();
            appKey = config.elementTextTrim("appKey");
            masterSecret = config.elementTextTrim("masterSecret");
            apnsProduction = (config.elementTextTrim("apnsProduction").equals("true"))?true:false;
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static JSONObject push(JSONObject params, ActionContext context){
        String registrationId = params.getString("registrationId");
        try {
            sendPushMessage(registrationId, appKey, masterSecret);
        } catch (APIConnectionException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (APIRequestException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return null;
    }

    public static JSONObject pushAlias(JSONObject params, ActionContext context){
        String alias = params.getString("alias");
        String mess = params.getString("sendMessage");
        try {
                sendPushMessageAlias(alias,mess, appKey, masterSecret);
        } catch (APIConnectionException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (APIRequestException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return null;
    }


    public static ScheduleResult sendPushMessage(String registrationId, String key, String secret) throws APIConnectionException, APIRequestException{
        ClientConfig config = ClientConfig.getInstance();
        HttpProxy proxy = null;
        if (useProxy) {
            proxy = new HttpProxy("http-proxy.system", 3128);
        }
        jpushClient = new JPushClient(secret, key, 3, proxy, config);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar nowTime = Calendar.getInstance();

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.registrationId(registrationId))
                .setNotification(Notification.alert("Android测试.." + nowTime.getTime()))
                .build();
        payload.resetOptionsTimeToLive(86400);
        payload.resetOptionsApnsProduction(apnsProduction);

        nowTime.add(Calendar.SECOND, 10);
        String scheduleTime =sdf.format(nowTime.getTime());

        ScheduleResult result = jpushClient.createSingleSchedule(UUID.randomUUID().toString().replaceAll("-", ""), scheduleTime, payload);
        return result;
    }


    public static ScheduleResult sendPushMessageAlias(String alias, String mess, String key, String secret) throws APIConnectionException, APIRequestException{
        ClientConfig config = ClientConfig.getInstance();
        HttpProxy proxy = null;
        if (useProxy) {
            proxy = new HttpProxy("http-proxy.system", 3128);
        }
        Set<String> tags = new HashSet<String>();
        if (alias != null && alias.length() > 0) {
            String[] arrys = alias.split(";");
            for (String str : arrys) {
                tags.add(str);
            }
        }
        jpushClient = new JPushClient(secret, key, 3, proxy, config);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar nowTime = Calendar.getInstance();

        String extrasparam = "{a:1}";
                PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(tags))
                .setNotification(Notification.newBuilder()
//                        .setAlert("1111")
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(mess)
                                .setTitle("订单通知")
                                //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                .addExtra("androidNotification extras key",extrasparam)
                                .build()
                        )
                        .addPlatformNotification(IosNotification.newBuilder()
                                //传一个IosAlert对象,指定apns title、title、subtitle等
                                .setAlert(mess)
                                //直接传alert
                                //此项是指定此推送的badge自动加1
                                .incrBadge(1)
                                //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
                                // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
                                .setSound("default")
                                //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                .addExtra("iosNotification extras key",extrasparam)
                                //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_ ... remote-notification
                                // .setContentAvailable(true)

                                .build()
                        )
                        .build()
                )
                //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
                // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/
                // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
                .setMessage(Message.newBuilder()
                        .setMsgContent("1111")
                        .setTitle("1111")
                        .addExtra("message extras key",extrasparam)
                        .build())

                .setOptions(Options.newBuilder()
                        //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                        .setApnsProduction(false)
                        //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                        .setSendno(1)
                        //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
                        .setTimeToLive(86400)
                        .build()
                )
                .build();
        payload.resetOptionsTimeToLive(86400);
        payload.resetOptionsApnsProduction(apnsProduction);

        nowTime.add(Calendar.SECOND, 10);
        String scheduleTime =sdf.format(nowTime.getTime());

        ScheduleResult result = jpushClient.createSingleSchedule(UUID.randomUUID().toString().replaceAll("-", ""), scheduleTime, payload);
        return result;
    }

    }


欢迎各位加群讨论:http://bbs.wex5.com/forum.php?mod=viewthread&tid=105046&page=1&extra=#pid165306780

89

主题

627

帖子

4216

积分

论坛元老

Rank: 8Rank: 8

积分
4216
QQ
发表于 2016-10-24 17:33:00 | 显示全部楼层
你好,请问这个可以再通知栏显示吗?
时也运也命也,非吾之所能也。
回复 支持 反对

使用道具 举报

89

主题

627

帖子

4216

积分

论坛元老

Rank: 8Rank: 8

积分
4216
QQ
发表于 2016-10-25 08:41:34 | 显示全部楼层
你好 使用你的这个方法 在app里调用这个接口,成功之后app会自动退出 然后在通知栏里接收到通知?
请问这是什么情况
时也运也命也,非吾之所能也。
回复 支持 反对

使用道具 举报

94

主题

403

帖子

683

积分

高级会员

Rank: 4

积分
683
QQ
 楼主| 发表于 2016-10-25 13:47:26 | 显示全部楼层
少翊 发表于 2016-10-25 08:41
你好 使用你的这个方法 在app里调用这个接口,成功之后app会自动退出 然后在通知栏里接收到通知?
请问这是 ...

不太明白你说的 ,这个示例代码仅供参考使用,
欢迎各位加群讨论:http://bbs.wex5.com/forum.php?mod=viewthread&tid=105046&page=1&extra=#pid165306780
回复 支持 反对

使用道具 举报

94

主题

403

帖子

683

积分

高级会员

Rank: 4

积分
683
QQ
 楼主| 发表于 2016-10-25 13:48:01 | 显示全部楼层
少翊 发表于 2016-10-25 08:41
你好 使用你的这个方法 在app里调用这个接口,成功之后app会自动退出 然后在通知栏里接收到通知?
请问这是 ...

APP会自动退出啥意思 ,我这边好像没遇到过这个情况
欢迎各位加群讨论:http://bbs.wex5.com/forum.php?mod=viewthread&tid=105046&page=1&extra=#pid165306780
回复 支持 反对

使用道具 举报

89

主题

627

帖子

4216

积分

论坛元老

Rank: 8Rank: 8

积分
4216
QQ
发表于 2016-10-26 09:28:45 | 显示全部楼层
wex81997167 发表于 2016-10-25 13:48
APP会自动退出啥意思 ,我这边好像没遇到过这个情况

我用你的这个java 在app中点击按钮去请求发送通知到手机
app先自动退出 然后在提示栏出现通知
时也运也命也,非吾之所能也。
回复 支持 反对

使用道具 举报

94

主题

403

帖子

683

积分

高级会员

Rank: 4

积分
683
QQ
 楼主| 发表于 2016-10-26 21:07:03 | 显示全部楼层
少翊 发表于 2016-10-26 09:28
我用你的这个java 在app中点击按钮去请求发送通知到手机
app先自动退出 然后在提示栏出现通知 ...

是崩溃了吗? 我是3.5正式版的,你看下日志什么的有没有报错信息,这样我看不出来
欢迎各位加群讨论:http://bbs.wex5.com/forum.php?mod=viewthread&tid=105046&page=1&extra=#pid165306780
回复 支持 反对

使用道具 举报

73

主题

292

帖子

1410

积分

金牌会员

Rank: 6Rank: 6

积分
1410
QQ
发表于 2016-11-2 14:10:24 | 显示全部楼层
正好需要这个功能,谢谢分享
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2025-6-18 15:40 , Processed in 0.114929 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表