gpt4 book ai didi

android - 解析推送通知notify twice for Android

转载 作者:行者123 更新时间:2023-11-29 01:50:12 26 4
gpt4 key购买 nike

我的 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());

}
}

但是当我使用这个 json 消息时,我收到 2 个通知:

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

所以,我不得不使用这个:

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

这就是我的 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;
}

}

有什么方法可以阻止应用程序在我使用警报时通知两次?即使我使用的是自定义接收器,也会以某种方式调用 defaultpushcallback。

提前致谢。

最佳答案

是的,替换:

PushService.setDefaultPushCallback(this, YourActivity.class);

到:

PushService.startServiceIfRequired(this);

Parse BroadcastReceiver 不再接收通知。

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

Android - Show Custom Notification without default notification- Parse.com

关于android - 解析推送通知notify twice for Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18661489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com