【发布时间】:2017-07-26 05:39:53
【问题描述】:
基于this guide,我尝试将我现有的Android 应用程序与OneSignal 集成。我将OneSignal 代码放在 MainActivity.java 中:
protected void onCreate(Bundle pSavedInstanceState)
{
OneSignal.startInit(this)
.setNotificationOpenedHandler(new OneSignalNotificationOpenedHandler())
.setNotificationReceivedHandler(new OneSignalReceivedHandler())
.init();
}
当然,我已经为.setNotificationOpenedHandler 和.setNotificationReceivedHandler 创建了类处理程序
类处理程序区域(在 MainActivity.java 中):
private class OneSignalReceivedHandler implements OneSignal.NotificationReceivedHandler
{
@Override
public void notificationReceived(OSNotification notification) {
JSONObject data = notification.payload.additionalData;
String customKey;
Log.d("onesignal=","onesignal receiver");
if (data != null) {
customKey = data.optString("customkey", null);
if (customKey != null)
Log.i("OneSignalExample", "customkey set with value: " + customKey);
}
}
}
private class OneSignalNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
{
@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;
Log.d("onesignal=","onesignal open handler");
if (data != null) {
customKey = data.optString("customkey", null);
if (customKey != null)
Log.i("OneSignalExample", "customkey set with value: " + customKey);
}
if (actionType == OSNotificationAction.ActionType.ActionTaken)
Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);
}
}
但在收到推送通知后,我的应用程序突然关闭并显示错误:java.lang.NullPointerException: Attempt to invoke virtual method
收到推送通知后的错误:
致命异常:pool-13-thread-1 进程:com.myapps,PID:24189 java.lang.NullPointerException:尝试调用虚拟方法 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' 开启 空对象引用 com.mylib.FCMMessageReceiverService.onMessageReceived(FCMMessageReceiverService.java:26) 在 com.google.firebase.messaging.FirebaseMessagingService.zzo(未知 来源)在 com.google.firebase.messaging.FirebaseMessagingService.zzn(未知 来源)在 com.google.firebase.messaging.FirebaseMessagingService.zzm(未知 来源)在 com.google.firebase.iid.zzb$2.run(未知来源)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 在 java.lang.Thread.run(Thread.java:818)
有什么想法吗?
非常感谢...
===更新
我的 MyFirebaseMessagingService.java 类:
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//super.onMessageReceived(remoteMessage);
Intent intent = new Intent(this, GameActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notifictionBuilder = new NotificationCompat.Builder(this);
notifictionBuilder.setContentTitle("My Application");
notifictionBuilder.setContentText(remoteMessage.getNotification().getBody());
notifictionBuilder.setAutoCancel(true);
notifictionBuilder.setSmallIcon(R.drawable.ic_launcher);
notifictionBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notifictionBuilder.build());
}
}
【问题讨论】:
-
你检查我的解决方案了吗??
-
嗨,是的,谢谢。但这里太晚了。如果我有更多问题,我会回复:)
标签: android push-notification onesignal