我正在尝试为我的后端服务器服务添加苹果推送通知,但我遇到了一个我无法理解的问题。
我正在使用 Pushy 库 (com.relayriders.pushy) .
我的代码就像他们的 github 页面推荐的那样。
代码是有效的,没有异常(exception),推送看起来像是正确形成的。推送发送到 APNS,但永远不会到达设备。
设备 token 和证书是正确的,我的 friend 通过他的测试程序向目标设备发送了推送。
我还测试了 com.notnoop.apns结果相同的库 - 没有异常(exception),仍然没有推送到设备
这是我的发件人类:
package ua.asprelis.communicator.push;
import com.relayrides.pushy.apns.ApnsEnvironment;
import com.relayrides.pushy.apns.FailedConnectionListener;
import com.relayrides.pushy.apns.PushManager;
import com.relayrides.pushy.apns.PushManagerConfiguration;
import com.relayrides.pushy.apns.RejectedNotificationListener;
import com.relayrides.pushy.apns.RejectedNotificationReason;
import com.relayrides.pushy.apns.util.ApnsPayloadBuilder;
import com.relayrides.pushy.apns.util.MalformedTokenStringException;
import com.relayrides.pushy.apns.util.SSLContextUtil;
import com.relayrides.pushy.apns.util.SimpleApnsPushNotification;
import com.relayrides.pushy.apns.util.TokenUtil;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLHandshakeException;
public class ApplePushNotifier
{
private final PushManager<SimpleApnsPushNotification> pushManager;
private final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();
public ApplePushNotifier() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException, IOException {
URL url = getClass().getResource("/certpath/cert.p12");
this.pushManager = new PushManager<>(
ApnsEnvironment.getSandboxEnvironment(),
SSLContextUtil.createDefaultSSLContext(URLDecoder.decode(url.getPath(), "UTF-8"), "password"),
null, // Optional: custom event loop group
null, // Optional: custom ExecutorService for calling listeners
null, // Optional: custom BlockingQueue implementation
new PushManagerConfiguration(),
"ExamplePushManager");
pushManager.start();
pushManager.registerRejectedNotificationListener(new MyRejectedNotificationListener());
pushManager.registerFailedConnectionListener(new MyFailedConnectionListener());
}
public void sendpush(String message, byte[] token) throws InterruptedException {
String payload = payloadBuilder.setAlertBody(message).setSoundFileName("ring-ring.aiff").buildWithDefaultMaximumLength();
pushManager.getQueue().put(new SimpleApnsPushNotification(token, payload));
}
public void sendpush(String message, String stoken) throws InterruptedException, MalformedTokenStringException {
byte[]token = TokenUtil.tokenStringToByteArray(stoken);
String payload = payloadBuilder.setAlertBody(message).setSoundFileName("ring-ring.aiff").buildWithDefaultMaximumLength();
SimpleApnsPushNotification notification = new SimpleApnsPushNotification(token, payload);
pushManager.getQueue().put(notification);
System.out.println("Queued: "+notification);
}
public void closeSender() {
if(pushManager!=null) {
try {
pushManager.shutdown();
} catch (InterruptedException ex) {
Logger.getLogger(ApplePushNotifier.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private class MyRejectedNotificationListener implements RejectedNotificationListener<SimpleApnsPushNotification> {
@Override
public void handleRejectedNotification(
final PushManager<? extends SimpleApnsPushNotification> pushManager,
final SimpleApnsPushNotification notification,
final RejectedNotificationReason reason) {
System.out.format("%s was rejected with rejection reason %s\n", notification, reason);
}
}
private class MyFailedConnectionListener implements FailedConnectionListener<SimpleApnsPushNotification> {
@Override
public void handleFailedConnection(
final PushManager<? extends SimpleApnsPushNotification> pushManager,
final Throwable cause) {
if (cause instanceof SSLHandshakeException)
// This is probably a permanent failure, and we should shut down the PushManager.
else
System.out.println(cause);
}
}
}
和运行推送的测试类:
@Test
public void testPushMessage() {
Random random = new Random();
String testMessage = "Hello Test! "+random.nextInt(1000);
String testToken = "e6878d3993abfaec48220b9d4d3ea0b576c22351c7fbbb5faeb5449bf7f24452";
//e6878d39 93abfaec 48220b9d 4d3ea0b5 76c22351 c7fbbb5f aeb5449b f7f24452
ApplePushNotifier notifier=null;
try {
notifier = new ApplePushNotifier();
notifier.sendpush(testMessage, testToken);
} catch(Exception ex) {
Logger.getLogger(applePushNotifierTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (notifier!=null)
notifier.closeSender();
}
}
我的输出:
[main] INFO com.relayrides.pushy.apns.PushManager - ExamplePushManager starting.
Queued: SimpleApnsPushNotification [token=[-26, -121, -115, 57, -109, -85, -6, -20, 72, 34, 11, -99, 77, 62, -96, -75, 118, -62, 35, 81, -57, -5, -69, 95, -82, -75, 68, -101, -9, -14, 68, 82], payload={"aps":{"alert":"Hello Test! 955","sound":"ring-ring.aiff"}}, invalidationTime=null, priority=IMMEDIATE]
[main] INFO com.relayrides.pushy.apns.PushManager - ExamplePushManager shutting down.
我觉得问题出在一些我看不到的小块上。如果两个测试库都不起作用,那就奇怪了
最佳答案
正如我所说 - 我犯了一个愚蠢的错误。我真是个笨蛋。我用过
ApnsEnvironment.getSandboxEnvironment()
代替
ApnsEnvironment.getProductionEnvironment()
在两个库中。
那是我第一次使用 APNS。
关于java - APNS 接受推送消息,但它们永远不会到达目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28220275/
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
当我执行>rvminstall1.9.2时一切顺利。然后我做>rvmuse1.9.2也很顺利。但是当涉及到ruby-v时..sam@sjones:~$rvminstall1.9.2/home/sam/.rvm/rubies/ruby-1.9.2-p136,thismaytakeawhiledependingonyourcpu(s)...ruby-1.9.2-p136-#fetchingruby-1.9.2-p136-#downloadingruby-1.9.2-p136,thismaytakeawhiledependingonyourconnection...%Total%Rece
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=