简体   繁体   English

解析推送通知两次通知Android

[英]Parse push notification notify twice for Android

I have a custom receiver for my android app that looks like this: 我的Android应用有一个自定义接收器,如下所示:

public class CustomReceiver extends BroadcastReceiver{
String URL;
String Title;
String Message;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
final String TAG = "MyCustomReceiver";

try {
String action = intent.getAction();
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
URL = json.getString("url");
Title = json.getString("title");
Message = json.getString("message");
showNotification(context);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
private void showNotification(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("URL", URL);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intent, 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(Title)
.setContentText(Message);
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());

} 
}

But when i use this json message, I get 2 notification : 但是当我使用此json消息时,我收到2条通知:

{
"title":"Test Message",
"alert":"See Facebook Webpage",
"url": "http://www.facebook.com/",
"action":"nsreceiver"
}

so, i am forced to use this: 因此,我被迫使用:

{
"title":"Test Message",
"message":"See Facebook Webpage",
"url": "http://www.facebook.com/",
"action":"nsreceiver"
}

This is how my MainActivityClass Looks Like: 这是我的MainActivityClass的样子:

import java.lang.reflect.Field;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.PushService;

public class MainActivity extends Activity {
    String previousUrl;
    String currentUrl;
    WebView WV;
    String INTurl = "";
    String AndroidDeviceId;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Parse.initialize(this, "Censored",
                "Censored");

        Intent GetExtraIntent = getIntent();
        if (GetExtraIntent != null) {
            INTurl = GetExtraIntent.getStringExtra("URL");
        }
        String defurl = "http://www.google.com/";

        PushService.setDefaultPushCallback(this, MainActivity.class);
        ParseInstallation.getCurrentInstallation().saveInBackground();
        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class
                    .getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception ex) {
            // Ignore
        }
        WV = (WebView) findViewById(R.id.WebView);
        WV.setInitialScale(1);
        WV.getSettings().setBuiltInZoomControls(true);
        WV.getSettings().setJavaScriptEnabled(true);
        WV.getSettings().setLoadWithOverviewMode(true);
        WV.getSettings().setUseWideViewPort(true);
        WV.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        WV.setHorizontalScrollBarEnabled(true);
        WV.setVerticalScrollBarEnabled(true);
        AndroidDeviceId = Secure.getString(this.getContentResolver(),
                Secure.ANDROID_ID);
//      Log.d("NewStationVIP- Android ID", AndroidDeviceId);
        final CookieManager CM;
        if (INTurl == null) {
            WV.loadUrl(defurl);
            CM = CookieManager.getInstance();
            CM.setCookie("http://www.google.com/", "deviceid" + "=" + AndroidDeviceId +";");
            CookieSyncManager.getInstance().sync();
        } else if (INTurl != null) {
            WV.loadUrl(INTurl);
            CM = CookieManager.getInstance();
            CM.setCookie("http://www.google.com/", "deviceid" + "=" + AndroidDeviceId +";");
            CookieSyncManager.getInstance().sync();

        }
        WebViewClient mWebClient = new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                WV.loadUrl(url);

                return true;
            }
        };
        WV.setWebViewClient(mWebClient);

    }

    @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.back:
            if (WV.canGoBack()) {
                // WV.canGoBack();
                WV.goBack();
            } else if (!WV.canGoBack()) {
                Toast.makeText(getApplicationContext(), "Can't go back",
                        Toast.LENGTH_LONG).show();
            }

            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return false;
    }

}

Is there any way to stop the app from notificating twice when i use alert? 当我使用警报时,有什么方法可以阻止应用程序两次通知吗? Somehow, defaultpushcallback gets called even though i am using custom receivers. 以某种方式,即使我使用自定义接收器,也会调用defaultpushcallback。

Thanks in advance. 提前致谢。

Yes, replace : 是的,更换:

PushService.setDefaultPushCallback(this, YourActivity.class);

to : 至 :

PushService.startServiceIfRequired(this);

Parse BroadcastReceiver no longer receive notifications. 解析BroadcastReceiver不再接收通知。

https://www.parse.com/questions/how-suppress-push-notification-from-being-displayed https://www.parse.com/questions/how-suppress-push-notification-from-being-displayed

Android - Show Custom Notification without default notification- Parse.com Android-在没有默认通知的情况下显示自定义通知-Parse.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM