Android 解析重新安装后显示的推送通知
Android Parse push notifications showing after reinstall
我在我的项目中使用 Parse 推送通知。通知在整个应用程序中运行良好。
客户端必须通过手动添加来订阅频道。频道保存在共享首选项数组中。我们不使用用户帐户。
问题:每当我重新安装应用程序(从而清除共享首选项/频道)时,在 Parse 服务器与频道列表(空)同步之前,我仍然会收到一次或两次推送通知。
我基本上在空频道列表上收到来自 Parse 的 1-2 或 3 个推送通知...
我通过测试发现的内容如下:我使用了正确的客户端+应用程序密钥进行首次安装。我添加了一些频道并在之后卸载了应用程序。在再次重新安装应用程序之前,我在代码中创建了组合键,并在错误的键下重新安装了应用程序……但我仍然可以在错误的键下(在整个应用程序中)收到通知……即使我收到了未经授权的异常。网站上的解析推送日志表明它没有将推送发送到任何设备,这绝对是错误的。
在我的申请中 class 为了安全起见,我确保取消订阅我的频道;
public class Application extends android.app.Application {
@Override
public void onCreate() {
Parse.initialize(this, "APP_ID", "CLIENT_ID");
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseInstallation install = ParseInstallation.getCurrentInstallation();
install.remove("channels");
install.saveInBackground();
super.onCreate();
}
}
我可以在应用内禁用通知。如果我这样做,然后重新安装应用程序,我将不会再收到错误推送。但我不能将该代码放在 onDestroy / onPause 中,这不是 "clean" 方式。
我通过在 Parse 上添加一些 JS Cloud 代码并在 Android 应用程序中添加自定义安装 ID 解决了这个问题。这可以防止新安装对象接收旧通知。
看来这个问题只出现在Android上。我在我的应用程序中添加了以下代码 (found online) 以使其工作:
Application.java
private void setupParse(Context context) {
Parse.initialize(this, <PARSE_APP_ID>, <PARSE_CLIENT_KEY>);
ParseInstallation.getCurrentInstallation().put("uniqueId", getWifiMacAddress(context));
ParseInstallation.getCurrentInstallation().saveInBackground();
}
private String getWifiMacAddress(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null && wifiManager.getConnectionInfo() != null) {
return wifiManager.getConnectionInfo().getMacAddress();
}
return "";
}
main.js
// Parse CloudCode
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {
Parse.Cloud.useMasterKey();
var uniqueId = request.object.get("uniqueId");
if (uniqueId == null || uniqueId == "") {
console.warn("No uniqueId found, exit");
response.success();
}
var query = new Parse.Query(Parse.Installation);
query.equalTo("uniqueId", uniqueId);
query.addAscending("createdAt");
query.find().then(function(results) {
for (var i = 0; i < results.length; ++i) {
if (results[i].get("installationId") != request.object.get("installationId")) {
console.warn("App id " + results[i].get("installationId") + ", delete!");
results[i].destroy().then(function() {
console.warn("Delete success");
},
function() {
console.warn("Delete error");
}
);
} else {
console.warn("Current App id " + results[i].get("installationId") + ", dont delete");
}
}
response.success();
},
function(error) {
response.error("Can't find Installation objects");
}
);
});
将代码部署到云端可能很困难,请按照本教程了解如何(Select 您在网站上的 OS):https://parse.com/apps/quickstart#cloud_code/
仔细阅读指南,因为它很容易搞砸,但这就是我将代码部署到云端所做的全部工作。
添加云代码后,登录以解析并select您的应用程序。然后在菜单 select 云中,它应该会显示来自 GitHub.
的已部署 JS (main.js)
之后为了测试目的你可以select "Data"在核心下监视安装对象,确保安装对象是固定的。
希望对您有所帮助:)
我在我的项目中使用 Parse 推送通知。通知在整个应用程序中运行良好。
客户端必须通过手动添加来订阅频道。频道保存在共享首选项数组中。我们不使用用户帐户。
问题:每当我重新安装应用程序(从而清除共享首选项/频道)时,在 Parse 服务器与频道列表(空)同步之前,我仍然会收到一次或两次推送通知。
我基本上在空频道列表上收到来自 Parse 的 1-2 或 3 个推送通知...
我通过测试发现的内容如下:我使用了正确的客户端+应用程序密钥进行首次安装。我添加了一些频道并在之后卸载了应用程序。在再次重新安装应用程序之前,我在代码中创建了组合键,并在错误的键下重新安装了应用程序……但我仍然可以在错误的键下(在整个应用程序中)收到通知……即使我收到了未经授权的异常。网站上的解析推送日志表明它没有将推送发送到任何设备,这绝对是错误的。
在我的申请中 class 为了安全起见,我确保取消订阅我的频道;
public class Application extends android.app.Application {
@Override
public void onCreate() {
Parse.initialize(this, "APP_ID", "CLIENT_ID");
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseInstallation install = ParseInstallation.getCurrentInstallation();
install.remove("channels");
install.saveInBackground();
super.onCreate();
}
}
我可以在应用内禁用通知。如果我这样做,然后重新安装应用程序,我将不会再收到错误推送。但我不能将该代码放在 onDestroy / onPause 中,这不是 "clean" 方式。
我通过在 Parse 上添加一些 JS Cloud 代码并在 Android 应用程序中添加自定义安装 ID 解决了这个问题。这可以防止新安装对象接收旧通知。
看来这个问题只出现在Android上。我在我的应用程序中添加了以下代码 (found online) 以使其工作:
Application.java
private void setupParse(Context context) {
Parse.initialize(this, <PARSE_APP_ID>, <PARSE_CLIENT_KEY>);
ParseInstallation.getCurrentInstallation().put("uniqueId", getWifiMacAddress(context));
ParseInstallation.getCurrentInstallation().saveInBackground();
}
private String getWifiMacAddress(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null && wifiManager.getConnectionInfo() != null) {
return wifiManager.getConnectionInfo().getMacAddress();
}
return "";
}
main.js
// Parse CloudCode
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {
Parse.Cloud.useMasterKey();
var uniqueId = request.object.get("uniqueId");
if (uniqueId == null || uniqueId == "") {
console.warn("No uniqueId found, exit");
response.success();
}
var query = new Parse.Query(Parse.Installation);
query.equalTo("uniqueId", uniqueId);
query.addAscending("createdAt");
query.find().then(function(results) {
for (var i = 0; i < results.length; ++i) {
if (results[i].get("installationId") != request.object.get("installationId")) {
console.warn("App id " + results[i].get("installationId") + ", delete!");
results[i].destroy().then(function() {
console.warn("Delete success");
},
function() {
console.warn("Delete error");
}
);
} else {
console.warn("Current App id " + results[i].get("installationId") + ", dont delete");
}
}
response.success();
},
function(error) {
response.error("Can't find Installation objects");
}
);
});
将代码部署到云端可能很困难,请按照本教程了解如何(Select 您在网站上的 OS):https://parse.com/apps/quickstart#cloud_code/
仔细阅读指南,因为它很容易搞砸,但这就是我将代码部署到云端所做的全部工作。
添加云代码后,登录以解析并select您的应用程序。然后在菜单 select 云中,它应该会显示来自 GitHub.
的已部署 JS (main.js)之后为了测试目的你可以select "Data"在核心下监视安装对象,确保安装对象是固定的。
希望对您有所帮助:)