Skip to content

Updated code sample in Java README.md#75

Open
dombartenope wants to merge 1 commit intoOneSignal:mainfrom
dombartenope:main
Open

Updated code sample in Java README.md#75
dombartenope wants to merge 1 commit intoOneSignal:mainfrom
dombartenope:main

Conversation

@dombartenope
Copy link
Copy Markdown

Issues in README.md

  • Getting started, when copied directly into an editor will return errors due to old naming conventions of properties on the Notification class as well as missing definitions for variables being declared

Java Client:

StringMap Error (missing property)

StringMap does not exist in the current context of v5.3.0

image - It should instead be `LanguageStringMap` - There is no longer a StringMap property in the public class `Notification` as it stands today

Arrays.asList() warning

image - Is mutability needed here? I can see that List.of `notification.setIncludedSegments(List.of(new String[]{"JavaTest", "Subscribed Users"}));` doesn't show any warning or errors here. From what I'm reading in the util docs, it seems Arrays.asList is only needed if we wanted to add

symbol 'api' error

image - Should be `DefaultApi api = new DefaultApi` or needs to be declared as that type before hand

Suggested changes

package org.example;  
  
// Import classes:  
import com.onesignal.client.ApiClient;  
import com.onesignal.client.ApiException;  
import com.onesignal.client.Configuration;  
import com.onesignal.client.auth.*;  
import com.onesignal.client.model.*;  
import com.onesignal.client.api.DefaultApi;  
  
import java.util.Arrays;  
import java.util.List;  
import java.util.Map;  
import java.util.HashMap;  
  
public class Main {  
    private static final String appId = "";  
    private static final String restApiKey = "";  
    private static final String organizationApiKey = "";  
  
    private static Notification createNotification() {  
        Notification notification = new Notification();  
        notification.setAppId(appId);  
        notification.setIsChrome(true);  
        notification.setIsAnyWeb(true);  
        notification.setIncludedSegments(List.of(new String[]{"Subscribed Users", "JavaTest"}));  
        LanguageStringMap contentStringMap = new LanguageStringMap();  
        contentStringMap.en("Working example");  
        notification.setContents(contentStringMap);  
        return notification;  
    }  
  
    public static void main(String[] args) {  
        // Setting up the client  
        ApiClient defaultClient = Configuration.getDefaultApiClient();  
        HttpBearerAuth restApiAuth = (HttpBearerAuth) defaultClient.getAuthentication("rest_api_key");  
        restApiAuth.setBearerToken(restApiKey);  
        HttpBearerAuth organizationApiAuth = (HttpBearerAuth) defaultClient.getAuthentication("organization_api_key");  
        organizationApiAuth.setBearerToken(organizationApiKey);  
        DefaultApi api = new DefaultApi(defaultClient);  
  
        // Setting up the notification  
        Notification notification = createNotification();  
        try {  
            // Sending the request  
            CreateNotificationSuccessResponse response = api.createNotification(notification);  
  
            // Checking the result  
            System.out.print(response);  
        } catch(Exception ex) {  
            ex.printStackTrace();  
        }  
    }  
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant