Открывайте приложение для Android в режиме просмотра веб-сайтов при нажатии на оповещение о сигнале - PullRequest
0 голосов
/ 01 мая 2018

Может кто-нибудь сказать мне, что я скучаю. У меня есть приложение для просмотра веб-страниц. Он работает нормально, за исключением этого, когда я закрываю приложение, уведомление о сигнале приходит нормально, но когда я нажимаю уведомление, приложение не открывается. Вот некоторый код, связанный с этой проблемой. Я искал решение, но все, что я пробовал, не работает. Когда приложение работает, уведомление приходит и открывается нормально, но когда оно закрыто, оно не запустит приложение. Спасибо.

 protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       OneSignal.startInit(this)
           .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
           .unsubscribeWhenNotificationsAreDisabled(true)
           .setNotificationOpenedHandler(new 
                      OneSignal.NotificationOpenedHandler() {
                @Override
                public void notificationOpened(OSNotificationOpenResult 
                                               result) {
                    String launchURL = 
                               result.notification.payload.launchURL;

                    Intent intent = new Intent(getApplicationContext(), 
                               NotificationActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
                            | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("url", launchURL);
                    startActivity(intent);
                }
            })
            .init();

}

public class NotificationExtender extends NotificationExtenderService {

@Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult notification) {
    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.
            return builder.setColor(new BigInteger("#005aaa", 16).intValue())
                    .setSmallIcon(R.drawable.ic_stat_onesignal_default);
        }
    };

    OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
    Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);

    return true;
}

}

 <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".NotificationExtender"
        android:permission="android.permission.BIND_JOB_SERVICE"
        android:exported="false">
        <intent-filter>
            <action android:name="com.onesignal.NotificationExtender" />
        </intent-filter>
    </service>

    <activity
        android:name=".NotificationActivity"
        android:parentActivityName=".MainActivity"/>

    <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />

1 Ответ

0 голосов
/ 02 мая 2018

Я решил это. Похоже, вам не нужен NotificationOpenedHandler, просто NotificationExtender расширяет NotificationExtenderService. Это выглядит так:

public class NotificationExtender extends NotificationExtenderService {

public NotificationExtender() {
    super();
}
@Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
    Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra("url", receivedResult.payload.launchURL);

    PendingIntent pendingIntent = PendingIntent.getActivity(
            getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

    Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.ic_stat_onesignal_default)
            .setSound(uri)
            .setContentTitle(receivedResult.payload.title)
            .setContentText(receivedResult.payload.body)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_onesignal_large_icon_default))
            .setColor(getResources().getColor(R.color.logo_color))
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

    return true;
}

}

...