点击OneSignal的通知不会打开结果活动!!!!! - 安卓 [英] Tapping a Notification from OneSignal does not open a result activity!!!! - Android

查看:102
本文介绍了点击OneSignal的通知不会打开结果活动!!!!! - 安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我知道对此有很多疑问,但是我一点也没有碰到运气,因此想在这里提一个问题.

Now I know there are lot of questions on this, but I have faced no luck at all and thought to ask a question here.

我有一个仅运行启动屏幕,然后运行MainActivity(仅是WebView)的应用程序 现在,我将其与OneSignal集成在一起以接收推送通知.

I have an application which just runs a splashscreen followed by a MainActivity(Which is just a WebView) Now I integrated this with OneSignal for receiving push notifications.

一切正常,我的意思是当我通过onesignal网站发送到我的手机时收到一条通知-但是我所面临的是,点击通知并没有得到我的ResultActivity(只是一个显示Toast消息的活动).

Everything works good, I mean I get a notification when sent through the onesignal website to my phone - but the thing I am facing is, tapping the notification does not get my ResultActivity(Just a activity displaying a Toast of message).

我的代码段如下所示:

splashscreen.java:

splashscreen.java:

public class splashscreen extends Activity {
@Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.splash);
      Thread splashThread = new Thread() {
         @Override
         public void run() {
            try {
               int waited = 0;
               while (waited < 5000) {
                  sleep(100);
                  waited += 100;
               }
            } catch (InterruptedException e) {
               // do nothing
            } finally {
               finish();
               Intent i = new Intent();
               i.setClassName("com.google",
                              "com.google.Main");
               startActivity(i);
            }
         }
      };
      splashThread.start();
   }
   @Override
   protected void onPause() {
       super.onPause();
       OneSignal.onPaused();
   }
   @Override
   protected void onResume() {
       super.onResume();
       OneSignal.onResumed();
   }
} 

Main.java:

Main.java:

@SuppressLint("SetJavaScriptEnabled") public class Main extends Activity     {
    /** Called when the activity is first created. */

    WebView web;
    private static Activity currentActivity;

    Intent resultIntent = new Intent(this, ResultActivity.class);


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        currentActivity = this;

        web = (WebView) findViewById(R.id.my_webview);
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://google.com");

        OneSignal.init(this, "xxxxxxx", "xxx-xxx-xxxx-xxxx-xxxxxx", new ExampleNotificationOpenedHandler());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
        }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.exit:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            String url2="http://google.com";
             // all links  with in ur site will be open inside the webview 
             //links that start with your domain example(http://www.example.com/)
            if (url != null && url.startsWith(url2)){
                return false;
                } 
           // all links that points outside the site will be open in a normal android browser
          else  {
                view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
                }
        }
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
  {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
        web.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
   }

   @Override
   protected void onPause() {
       super.onPause();
       OneSignal.onPaused();
   }
   @Override
   protected void onResume() {
       super.onResume();
       OneSignal.onResumed();
   }



// NotificationOpenedHandler is implemented in its own class instead of adding implements to MainActivity so we don't hold on to a reference of our first activity if it gets recreated.
private class ExampleNotificationOpenedHandler implements 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 activity 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 messageTitle = "OneSignal Example" + isActive, messageBody = message;


    try {
      if (additionalData != null) {
        if (additionalData.has("title"))
          messageTitle = additionalData.getString("title");
        if (additionalData.has("actionSelected"))
          messageBody += "\nPressed ButtonID: " + additionalData.getString("actionSelected");

        messageBody = message + "\n\nFull additionalData:\n" + additionalData.toString();
      }
    } catch (JSONException e) { }
    /*
    new AlertDialog.Builder(Main.currentActivity)
                   .setTitle(messageTitle)
                   .setMessage(messageBody)
                   .setCancelable(true)
                   .setPositiveButton("OK", null)
                   .create().show();
    */
    NotificationCompat.Builder mBuilder =
       new NotificationCompat.Builder(Main.currentActivity)
            .setSmallIcon(R.drawable.cc)
            .setContentTitle(messageTitle)

            .setDefaults(
                        Notification.DEFAULT_SOUND
                                | Notification.DEFAULT_VIBRATE
                                | Notification.FLAG_AUTO_CANCEL)  

            .setContentText(messageBody);

    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);



    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
            Main.currentActivity,
            0,
            resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
            | PendingIntent.FLAG_ONE_SHOT
        );    


    mBuilder.setContentIntent(resultPendingIntent);

   NotificationManager mNotifyMgr = 
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

   mNotifyMgr.notify(001, mBuilder.build());

  }
}
}

ResultActivity.java:

ResultActivity.java:

    public class ResultActivity extends Activity {
        /** Called when the activity is first created. */
         @Override
         protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          /*
          ImageView image = new ImageView(this);
          image.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
          setContentView(image);
          Toast.makeText(getApplicationContext(), 
            "Do Something NOW", 
            Toast.LENGTH_LONG).show();
            */
          super.onCreate(savedInstanceState);
          setContentView(R.layout.splash);
          }
    }

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.google.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.google.permission.C2D_MESSAGE" />



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

        <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />
        <activity android:name="com.onesignal.NotificationOpenedActivity"  android:theme="@android:style/Theme.NoDisplay">
            </activity>


        <receiver
         android:name="com.onesignal.GcmBroadcastReceiver"
         android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.google" />
            </intent-filter>
        </receiver>
        <service android:name="com.onesignal.GcmIntentService" />

        <activity android:name=".splashscreen" android:label="@string/app_name">
                <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


        <activity android:name=".Main" android:label="@string/app_name" > 

        </activity>


        <activity android:name=".ResultActivity"
           android:label="@string/app_name"
           android:exported="true">
        </activity>



         <receiver
            android:name="com.google.OneSignalBackgroundDataReceiver"
            android:exported="false">
            <intent-filter>
            <action android:name="com.onesignal.BackgroundBroadcast.RECEIVE" />
            </intent-filter>
         </receiver>


    </application>

</manifest>

我已经尝试了所有这些答案,但是都没有用:

I have tried all these answers but none worked:

通知在打开时未打开Acivity

Android状态栏通知-打开选择通知时正确的活动

Android:在推送通知上点击不会打开应用程序

Android点击通知无法打开附件活动

修改一条线并在设备上对其进行测试非常痛苦!由于onesignal仅允许在设备中进行测试.请帮助或至少指导我进行调试.

It is painfull to modify one line and test it on device! Since onesignal allows only testing in device. Please help or atleast guide me how to debug.

经过apk测试的设备:运行Lolipop的Samsung Galaxy S4.

Device on which the apk was tested : Samsung Galaxy S4 running Lolipop.

推荐答案

看一下此链接: https://documentation.onesignal.com/docs /android-customizations#section-background-data-and-notification-overriding (在页面中搜索:"更改通知的打开操作"以转到确切的段落).

Take a look at this link: https://documentation.onesignal.com/docs/android-customizations#section-background-data-and-notification-overriding (Search : "Changing the open action of a notification" in the page to go to the exact paragraph).

这是一个示例: http://androidbash.com/android-push-notification-service-using- onesignal/

我没有时间仔细阅读您的代码,但似乎有一些问题:

I don't have time to read your code carefully, but seems like it has some problems:

  1. 您在错误的位置初始化了OneSignal.

  1. You initialize OneSignal in the wrong place.

确保您使用初始化OneSignal 您的onCreate方法中的setNotificationOpenedHandler 应用类.您将需要从此回调中调用startActivity"(OneSignal的文档).

"Make sure you are initializing OneSignal with setNotificationOpenedHandler in the onCreate method in your Application class. You will need to call startActivity from this callback" (OneSignal's document).

您不需要 AndroidManifest 中的任何其他接收器即可捕获意图并打开目标活动 OneSignal.NotificationOpenedHandler 处理这个(事情.但是请不要忘记以下这一行,以防止OneSignal打开您的启动器活动:

You don't need any other receivers in AndroidManifest to catch intent and open your target activity, OneSignal.NotificationOpenedHandler already handle this. But don't forget this line to prevent OneSignal open your launcher activity:

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

我在我的应用程序中使用了此解决方案,并且效果很好.因为它就是这样.

I use this solution in my app and it works fine. Because it's the way it is.

这篇关于点击OneSignal的通知不会打开结果活动!!!!! - 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆