【发布时间】:2017-10-12 06:35:06
【问题描述】:
为了发送通知,我使用 Onesignal。我在设备中收到通知,但未显示在通知栏中。 我也可以获得通知并在警报对话框中显示。但是当我想在通知栏中显示时,不要在那里显示任何内容
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.gardle
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'
}
【问题讨论】:
-
我在某些小米设备上也遇到过类似的情况。你在什么设备上测试。
-
我在三星设备上测试过(注4)
-
我真的疯了。我努力解决这个问题。在我使用服务 Firebase 之前没有任何问题。
-
那么您的问题可能会有所不同。 OneSignal 有一个支持系统。尝试联系他们
标签: android notifications android-notifications onesignal