0

我使用 THES SORCE 获取和显示通知,但此代码不能自定义。我需要定制通知的来源 从 onesignal.com 收到

我需要通过 onesignal 发送 url 并接收并显示图标通知

package booke.vfj.angour.ir.baftani_davood;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.view.LayoutInflater;


import org.json.JSONObject;

import java.util.ArrayList;

public class G extends Application {

    public static Context               context;
    public static LayoutInflater        inflater;
    public static Activity              currentActivity;
    public static ArrayList<StructTest> Recordsfehrset = new ArrayList<StructTest>();
    public static ArrayList<StructTest2> recordjoziat = new ArrayList<StructTest2>();
    public static ArrayList<StructTest> fov = new ArrayList<StructTest>();

    @Override
    public void onCreate()
    {
        super.onCreate();
        context = getApplicationContext();
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        OneSignal.startInit(this).init();
        OneSignal.startInit(this).setNotificationOpenedHandler(new NotificationOpenedHandler()).init();
    }

    private class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
        @Override
        public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
            try {
                String messageTitle;
                AlertDialog.Builder builder = null;

                if (additionalData != null) {
                    if (additionalData.has("discount"))
                        messageTitle = "Discount!";
                    else if (additionalData.has("bonusCredits"))
                        messageTitle = "Bonus Credits!";
                    else
                        messageTitle = "Other Extra Data";

                    builder = new AlertDialog.Builder(getApplicationContext()).setTitle(messageTitle).setMessage(message + "\n\n" + additionalData.toString());
                }
                else if (isActive) // If a push notification is received when the app is being used it does not display in the notification bar so display in the app.
                    builder = new AlertDialog.Builder(getApplicationContext())
                            .setTitle("OneSignal Message")
                            .setMessage(message);

                // Add your app logic around this so the user is not interrupted during gameplay.
                if (builder != null)
                    builder.setCancelable(true)
                            .setPositiveButton("OK",null)
                            .create().show();

            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
}
4

1 回答 1

2

当点击通知时,OneSignal SDK 将负责设置标题、图标和打开 URL。

默认情况下,OneSignal 只会在您的应用不在焦点时在 Android 的通知区域中显示通知。如果您的应用程序处于焦点位置,则将调用 NotificationOpenedHandler 而不是显示通知。如果您宁愿总是显示通知,您可以OneSignal.enableNotificationsWhenActive(true);在初始化后调用。

您也可以删除OneSignal.startInit(this).init();它,因为它下面的行也在初始化 OneSignal。

于 2016-02-13T03:07:22.983 回答