Onesignal通知显示在OneSignal仪表板上,但不会在通知栏中显示(android)

时间:2017-10-12 06:35:06

标签: android notifications android-notifications onesignal

要发送通知我使用 Onesignal 。我会在设备中收到通知,但不会在通知栏中显示。 我也可以在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;
}
    }

所有MyApplication

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'
}

2 个答案:

答案 0 :(得分:0)

尝试这样,对我来说很好。

在你的Myplication类文件里面就像这样创建了。

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 + "\nFull additionalData:\n" + additionalData.toString();
            }

            Log.d("OneSignalExample" +additionalData + ",additionalData");
            Log.d("OneSignalExample", "message:\n" + message + "\nadditionalMessage:\n" + additionalMessage);
            ShopSmartApplication.getInstance().startActivity(in);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

最后在您的清单文件中,在应用程序标记

中声明以下内容
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />

答案 1 :(得分:0)

感谢Karthik。我犯了一个可怕的错误。我的手机上的设备不允许显示通知。代码没有问题。