OneSignal - 收到推送通知后出错
OneSignal - Get Error after receiving push Notif
基于 this guide,我尝试将我现有的 Android 应用程序与 OneSignal
集成。我把 OneSignal
代码放在 MainActivity.java :
protected void onCreate(Bundle pSavedInstanceState)
{
OneSignal.startInit(this)
.setNotificationOpenedHandler(new OneSignalNotificationOpenedHandler())
.setNotificationReceivedHandler(new OneSignalReceivedHandler())
.init();
}
当然,我已经为 .setNotificationOpenedHandler
和 .setNotificationReceivedHandler
创建了 class 处理程序
class处理程序区域(在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
收到推送通知后出现的错误:
FATAL EXCEPTION: pool-13-thread-1 Process: com.myapps, PID: 24189
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String
com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on
a null object reference at
com.mylib.FCMMessageReceiverService.onMessageReceived(FCMMessageReceiverService.java:26)
at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown
Source) at
com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown
Source) at
com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown
Source) at com.google.firebase.iid.zzb.run(Unknown Source) at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
有什么想法吗?
之前非常感谢...
===更新
我的MyFirebaseMessagingService.javaclass:
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());
}
}
如错误提示,
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String
com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on
a null object reference at
检查名为
的文件
MyFirebaseMessagingService
在其中你将拥有这个功能
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
在您的 $message 上添加一个通知对象。您的 POST 请求正文必须类似于:
{
"to" : "aUniqueKey",
"notification" : {
"body" : "Blah Blah!",
"title" : "Hey vs. Hello"
},
"data" : {
"Nick" : "Amigos",
"Room" : "Lasta vs Avista"
}
}
您的 remoteMessage.getNotification()
returnsnull
因为您的 POST 请求正文不包含通知对象。
基于 this guide,我尝试将我现有的 Android 应用程序与 OneSignal
集成。我把 OneSignal
代码放在 MainActivity.java :
protected void onCreate(Bundle pSavedInstanceState)
{
OneSignal.startInit(this)
.setNotificationOpenedHandler(new OneSignalNotificationOpenedHandler())
.setNotificationReceivedHandler(new OneSignalReceivedHandler())
.init();
}
当然,我已经为 .setNotificationOpenedHandler
和 .setNotificationReceivedHandler
创建了 class 处理程序
class处理程序区域(在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
收到推送通知后出现的错误:
FATAL EXCEPTION: pool-13-thread-1 Process: com.myapps, PID: 24189 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference at com.mylib.FCMMessageReceiverService.onMessageReceived(FCMMessageReceiverService.java:26) at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown Source) at com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown Source) at com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown Source) at com.google.firebase.iid.zzb.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
有什么想法吗?
之前非常感谢...
===更新
我的MyFirebaseMessagingService.javaclass:
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());
}
}
如错误提示,
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference at
检查名为
的文件MyFirebaseMessagingService
在其中你将拥有这个功能
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
在您的 $message 上添加一个通知对象。您的 POST 请求正文必须类似于:
{
"to" : "aUniqueKey",
"notification" : {
"body" : "Blah Blah!",
"title" : "Hey vs. Hello"
},
"data" : {
"Nick" : "Amigos",
"Room" : "Lasta vs Avista"
}
}
您的 remoteMessage.getNotification()
returnsnull
因为您的 POST 请求正文不包含通知对象。