Skip to content

Instantly share code, notes, and snippets.

@vtim
Created May 10, 2012 21:54
Show Gist options
  • Save vtim/2656146 to your computer and use it in GitHub Desktop.
Save vtim/2656146 to your computer and use it in GitHub Desktop.
Quick Groovy-script for sending push-messages to an iOS-device
@Grapes(
@Grab(group='com.notnoop.apns', module='apns', version='0.1.6')
)
import com.notnoop.apns.APNS
import com.notnoop.apns.ApnsService
import com.notnoop.apns.PayloadBuilder
import com.notnoop.exceptions.NetworkIOException
// Fill in these values
String certPath = "/path/to/certificate.p12"
String certPassword = ""
String deviceToken = ""
ApnsService apnsService = APNS.newService()
.withCert(certPath, certPassword)
.withProductionDestination() // or: .withSandboxDestination()
.build()
println "APNSService built."
def input
System.in.withReader { inReader ->
print 'input: '
input = inReader.readLine()
while (input != "") {
PayloadBuilder payloadBuilder = APNS.newPayload()
.alertBody(input)
.actionKey("Details") // action-loc-key, translated on device
.customField("someCustomField", "someCustomValue")
.sound("default")
if (payloadBuilder.isTooLong()) {
payloadBuilder.shrinkBody()
}
String payload = payloadBuilder.build()
apnsService.push(deviceToken, payload)
println "Pushed: $input"
print 'input: '
input = inReader.readLine()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment