Open jlugner opened 8 years ago
This only seems to happen when I import AppState from react-native in the same file as my notification-code. Any ideas here?
I just wrote about a possible workaround here: https://github.com/oney/react-native-gcm-android/issues/51#issuecomment-212933067
Comment the
GcmAndroid.stopService();
line.
It should work.
My index.android.js snippet:
`if (GcmAndroid.launchNotification) { console.log('Before entering function:', GcmAndroid.launchNotification); //GcmAndroid.stopService(); console.log('GcmAndroid.launchNotification:', GcmAndroid.launchNotification); var notification = GcmAndroid.launchNotification; var info = JSON.parse(notification.info); Notification.create({ subject: info.subject, message: info.message, });
//GcmAndroid.stopService(); } else { class Savio extends Component {`
@savioseb It still crashes!! any possible workaround?
As @udfalkso mentioned I have rewrite his code for workaround to get notification dynamically from GCM itself.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getBundleExtra("bundle");
Object result = bundle.get("info");
String data = result.toString();
String id = bundle.get("google.message_id").toString();
try{
JSONObject jsonObj = new JSONObject(data);
Log.d(TAG,"onStartCommand jsonObj => :"+jsonObj);
Log.d(TAG,"onStartCommand bundle => :"+bundle);
String message = jsonObj.getString("message");
String subject = jsonObj.getString("subject");
Integer notificationId = Integer.parseInt(id.substring(0, 10).replaceAll("[\\D]", ""));
NotificationAttributes attributes = new NotificationAttributes();
attributes.delayed = false;
attributes.scheduled = false;
attributes.autoClear = true;
attributes.inboxStyle = false;
attributes.priority = 2;
attributes.sound = "default";
attributes.smallIcon = "ic_launcher";
attributes.message = message;
attributes.subject = subject;
Notification notification = new Notification(this, notificationId, attributes);
notification.create();
}catch (JSONException e) {
Log.d(TAG,e);
}
return START_NOT_STICKY;
}
For some reason, my app crashes if I receive a push notification when the app is closed. It works just fine when the app is in foreground/background, but if I actually close it, notifications will cause a crash. My logs contain the following: