Soru & Cevap

OneSignal ile Push Notification problemi ...

19.06.2017 - 04:38

One signal ile bildirim gönderdiğimde bildirim çubuğuna bildirimim düşüyor. Ama uygulama tamamen kapalıyken tıkladığımda hiç bir şey açılmıyor. Uygulama arkaplanda açıkken veya önde açıkken gönderdiğim bildirime tıklayınca açılıyor. Openhandler ve receivedhandler kullandım webviewde acabilmek için. 
Sanırım servis eklemem gerekiyor arkaplanda hazır bekletebilmek için ama nereye eklemem gerektiğini bulamadım.

MainActivity kodlarım da burada

package d****.k****en;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.onesignal.OSNotification;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import android.app.Application;
import com.onesignal.OneSignal.NotificationOpenedHandler;

import org.json.JSONException;
import org.json.JSONObject;



public class MainActivity extends Activity {

    private WebView webView;
    private String Uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) { // Bu metod uygulama açıldığında çalıştırılan metod.

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String Uri = getIntent().getStringExtra("openURL");
        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
                .setNotificationReceivedHandler(new ExampleNotificationReceivedHandler())
                .init();

        // webView'i tasarımdakiyle bağlıyoruz.
        webView = (WebView) findViewById(R.id.webViewl);
        // webView'i JavaScript kodlarını çalıştıracak şekilde set ediyoruz.
        webView.getSettings().setJavaScriptEnabled(true);
        // Sayfanın yüklendiğinin anlaşılması için ProgressDialog açıyoruz.
        webView.setWebViewClient(new WebViewClient() {
            // Sayfa Yüklenirken bir hata oluşursa kullanıcıyı uyarıyoruz.
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                Toast.makeText(getApplicationContext(), "Sayfa Yüklenemedi!",
                        Toast.LENGTH_SHORT).show();
            }
            // Sayfanın yüklenme işlemi bittiğinde progressDialog'u kapatıyoruz.
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                findViewById(R.id.spscreen).setVisibility(View.GONE);
                //show webview
                findViewById(R.id.webViewl).setVisibility(View.VISIBLE);
            }
        });
        //Web sayfamızın url'ini webView'e yüklüyoruz.
        if (Uri == null) {
            webView.loadUrl("https://www.******.com");
        } else {
            webView.loadUrl(Uri);
        }
        // 


    }
    private class ExampleNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
        @Override
        public void notificationReceived(OSNotification notification) {
            JSONObject data = notification.payload.additionalData;
            String notificationID = notification.payload.notificationID;
            String title = notification.payload.title;
            String body = notification.payload.body;
            String smallIcon = notification.payload.smallIcon;
            String largeIcon = notification.payload.largeIcon;
            String bigPicture = notification.payload.bigPicture;
            String smallIconAccentColor = notification.payload.smallIconAccentColor;
            String sound = notification.payload.sound;
            String ledColor = notification.payload.ledColor;
            int lockScreenVisibility = notification.payload.lockScreenVisibility;
            String groupKey = notification.payload.groupKey;
            String groupMessage = notification.payload.groupMessage;
            String fromProjectNumber = notification.payload.fromProjectNumber;
            //BackgroundImageLayout backgroundImageLayout = notification.payload.backgroundImageLayout;
            String rawPayload = notification.payload.rawPayload;

            String customKey;

            Log.i("OneSignalExample", "NotificationID received: " + notificationID);

            if (data != null) {
                customKey = data.optString("customkey", null);
                if (customKey != null)
                    Log.i("OneSignalExample", "customkey set with value: " + customKey);
            }
        }
    }
    private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
        // This fires when a notification is opened by tapping on it.
        @Override
        public void notificationOpened(OSNotificationOpenResult result) {
            OSNotificationAction.ActionType actionType = result.action.type;
            JSONObject data = result.notification.payload.additionalData;
            String Uri = result.notification.payload.launchURL; // update docs launchUrl
            String customKey;
            String openURL = Uri;
            Object activityToLaunch = MainActivity.class;

            if (data != null) {
                customKey = data.optString("customkey", null);
                openURL = data.optString("openURL", null);

                if (customKey != null)
                    Log.i("OneSignalExample", "customkey set with value: " + customKey);

                if (openURL != null)
                    Log.i("OneSignalExample", "openURL to webview with URL value: " + openURL);
            }

            // The following can be used to open an Activity of your choice.
            // Replace - getApplicationContext() - with any Android Context.
            // Intent intent = new Intent(getApplicationContext(), YourActivity.class);
            Intent intent = new Intent(getApplicationContext(), (Class<?>) activityToLaunch );
            // İlk Hali intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
           // Direk kapanan hali intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);

            intent.putExtra("openURL", Uri);
            Log.i("OneSignalExample", "openURL = " + openURL);
            // startActivity(intent);
            startActivity(intent);
            


                      
            
            // Add the following to your AndroidManifest.xml to prevent the launching of your main Activity
            //   if you are calling startActivity above.
     /*
        <application ...>
          <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
        </application>
     */




        }
    }



    public void onBackPressed() //Android Back Buttonunu Handle ettik. Back butonu bir önceki sayfaya geri dönecek
    {
        if (webView.canGoBack()) {//eğer varsa bir önceki sayfaya gidecek
            webView.goBack();
        } else {//Sayfa yoksa uygulamadan çıkacak


            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
            alertDialogBuilder.setTitle("Çıkmak istediğinizden emin misiniz?");
            alertDialogBuilder

                    .setCancelable(false)
                    .setPositiveButton("Evet",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    MainActivity.super.onBackPressed();
                                }
                            })

                    .setNegativeButton("Hayır", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            dialog.cancel();
                        }
                    });

            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }

    }
}

 

204 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user ilkertr
ilkertr
07.10.2019 - 12:03

Çözüm bulabildinizmi?