极光推送-JAVA服务器端推送代码

1、根据别名推送Android设备

public void sendNotificationWirhAlias_Android(String title,String notification,String alias){
        try {
            PushPayload payload = PushPayload
                    .newBuilder()
                    .setPlatform(Platform.android_winphone())
                    .setAudience(Audience.alias(alias.replace(".", "_")))
                    .setNotification(Notification.newBuilder()
                    .setAlert(notification)
                    .addPlatformNotification(AndroidNotification.newBuilder()
                    .setTitle(title).build())
                    .build())
                    .build();
            PushResult result = client.sendPush(payload);
            System.out.println(result.isResultOK());
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

2、根据别名推送到IOS设备

public void sendNotificationWirhAlias_Ios(String notification,String alias)
    {
        try {
            PushPayload payload = PushPayload
                    .newBuilder()
                    .setPlatform(Platform.ios())
                    .setAudience(Audience.alias(alias.replace(".", "_")))
                    .setNotification(Notification.newBuilder()
                    .setAlert(notification)
                    .addPlatformNotification(IosNotification.newBuilder().setSound("happy.caf").setBadge(1).build())
                    .build()).setOptions(Options.newBuilder()
                             .setApnsProduction(true)
                             .build())
                    .build();
            PushResult result = client.sendPush(payload);
            System.out.println(result.isResultOK());
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

3、根据别名推送到所有平台

public boolean sendNotificationWithAlias(String title,String notification,String alias,Map extras){
        try {

            PushPayload payload = PushPayload  
            .newBuilder()  
            .setPlatform(Platform.all())  
            .setAudience(Audience.alias(alias.replace(".", "_")))  
            .setNotification(  
                    Notification  
                            .newBuilder()  
                            .addPlatformNotification(  
                                    IosNotification.newBuilder()  
                                            .setAlert(notification)
                                            .setSound("happy.caf").setBadge(1)
                                            .addExtras(extras).build())
                            .addPlatformNotification(  
                                    AndroidNotification.newBuilder()  
                                            .setAlert(notification)  
                                            .setTitle(title).addExtras(extras)  
                                            .build())  
                            .addPlatformNotification(  
                                    WinphoneNotification.newBuilder()  
                                            .setAlert(notification)  
                                            .addExtras(extras).build())  
                            .build()).setOptions(Options.newBuilder()
                                    .setApnsProduction(true)
                                    .build()).build();  

            PushResult result =client.sendPush(payload);
            System.out.println(result.isResultOK());
            return result.isResultOK();
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return false;
    }

你可能感兴趣的:(工作学习心得)