Reputation: 437
I have created demo app for One Signal Push notification. It's work fine on emulator but when testing on real device. There is one problem when application closed did not receive push notification.
implementation code is like as following:
TestDemo.java file
public class TestDemo extends Application {
@Override
public void onCreate() {
super.onCreate();
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.DEBUG, OneSignal.LOG_LEVEL.WARN);
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.autoPromptLocation(true)
.init();
}
private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;
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);
}
}
}
MainActivity.java file
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
@Override
public void idsAvailable(String userId, String registrationId) {
Log.d("UserId : ", userId);
Log.d("Reg Id : ", registrationId);
}
});
}
and also given permission as suggestion on documentation
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Please help if any solution for that...
Thanks...
Upvotes: 2
Views: 9955
Reputation: 12618
The following are reasons why notifications may show as delivered on the OneSignal dashboard or API, but are not visible on your device or website:
The app is currently In Focus
By default, notifications will not be displayed on the device if your app is currently "in focus" (open and visible). However, you can call inFocusDisplaying
with InAppAlert
to show notifications as alert boxes in your app, or Notification
to display the notification.
The app is Force Stopped
When an app is in a "Force Stopped" state most events including GCM/FCM messages for push notifications will not be received. An app can be placed in this state in the following ways.
App is closed on some Huawei, Xiaomi, or Sony devices due their custom Android tweaks. The following device settings can be changed to prevent this.
* Huawei - Go to Settings > "Protected apps", check your app.
* Xiaomi - Make sure "Auto-start" property enabled for your app in the settings.
* Sony - Tap on the battery icon. Go to Power Management > STAMINA mode > Apps active in standby > Add your app.
To confirm your app state is the issue send a few notifications and check for the following GCM logcat entry.
W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.onesignal.example (has extras) }
Some device manufactures will white list apps from going into the force closed state. Example such as Gmail and Whatsapp.
You have network issues
The network / WiFi you're connected to may have closed your connection to Google servers'. Try disabling and re-enabling your internet connection. See this post for more details.
Upvotes: 6