Onesignal通知显示在OneSignal仪表板上,但未在通知栏中显示(android)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Onesignal通知显示在OneSignal仪表板上,但未在通知栏中显示(android)相关的知识,希望对你有一定的参考价值。
对于发送通知我使用Onesignal.I在我的设备中收到通知但未在通知栏中显示。我也可以在Alert Dialog中获得通知并显示。但是当我想在通知栏中显示时,不显示任何内容
androidManifest.xml中
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/logo16"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Disbale opening of launcher Activity -->
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT"
android:value="DISABLE" />
<activity
android:name=".MainActivity"
android:screenOrientation="portrait" />
<service
android:name=".MyNotificationExtenderService"
android:exported="false">
<intent-filter>
<action android:name="com.onesignal.NotificationExtender" />
</intent-filter>
MyNotificationExtenderService
public class MyNotificationExtenderService extends NotificationExtenderService {
@Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
receivedResult.payload.lockScreenVisibility = Integer.parseInt("-1");
JSONObject data = receivedResult.payload.additionalData;
OverrideSettings overrideSettings = new OverrideSettings();
overrideSettings.extender = new NotificationCompat.Extender() {
@Override
public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
// Sets the background notification color to Green on Android 5.0+ devices.
builder = builder.setVisibility(Integer.parseInt("-1"));
builder = builder.setVibrate(new long[]{0L});
return builder.setColor(new BigInteger("800d2f66", 16).intValue());
}
};
OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);
return true;
}
}
我的应用程序
public class MyApplication extends Application {
private static Context context;
public static Context getContext() {
return context;
}
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
// OneSignal.setLogLevel(OneSignal.LOG_LEVEL.DEBUG, OneSignal.LOG_LEVEL.DEBUG);
//MyNotificationOpenedHandler : This will be called when a notification is tapped on.
//MyNotificationReceivedHandler : This will be called when a notification is received while your app is running.
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
}
}
的build.gradle
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.onesignal:OneSignal:[3.6.2, 3.99.99]'
compile 'com.android.support:cardview-v7:21.+'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
testCompile 'junit:junit:4.12'
}
答案
试试这样,它对我来说很好。
在你的Myplication类文件里面就像这样创建write。
OneSignal.startInit(this)
.setNotificationOpenedHandler(new SSNotificationOpenedHandler())
.setAutoPromptLocation(true)
.init();
现在创建SSNotificationOpenedHandler类文件和SSNotificationOpenedHandler中的代码,如下所示。
public class SSNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
/**
* Callback to implement in your app to handle when a notification is opened from the Android status bar or
* a new one comes in while the app is running.
* This method is located in this Application class as an example, you may have any class you wish implement NotificationOpenedHandler and define this method.
*
* @param message The message string the user seen/should see in the Android status bar.
* @param additionalData The additionalData key value pair section you entered in on onesignal.com.
* @param isActive Was the app in the foreground when the notification was received.
*/
@Override
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
String additionalMessage = "";
try {
Intent in = new Intent(MyApplication.getInstance(), MainActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
if (additionalData != null ) {
if (additionalData.has("link")) {
Log.d("OneSignalExample additionalData.getString("link") : " + additionalData.getString("link"));
in.setData(Uri.parse(additionalData.getString("link")));
}
additionalMessage = message + "
Full additionalData:
" + additionalData.toString();
}
Log.d("OneSignalExample" +additionalData + ",additionalData");
Log.d("OneSignalExample", "message:
" + message + "
additionalMessage:
" + additionalMessage);
ShopSmartApplication.getInstance().startActivity(in);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
最后在您的清单文件中,在application标记内声明以下内容
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
另一答案
感谢Karthik.I有一个可怕的错误。我的手机上的设置不允许显示通知。代码没有问题。
以上是关于Onesignal通知显示在OneSignal仪表板上,但未在通知栏中显示(android)的主要内容,如果未能解决你的问题,请参考以下文章