我有一个应用程序正在接收GCM通知并链接到此通知pendingIntent以打开URL:
FutureTask<String> futureTask = new FutureTask(
new MissingTextPrompt()
);
Platform.runLater(futureTask);
try {
password = futureTask.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
class MissingTextPrompt implements Callable<String> {
@Override public String call() throws Exception {
TextInputDialog dialog = new TextInputDialog(null);
dialog.setTitle("System password required");
dialog.setHeaderText(null);
dialog.setContentText(null);
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
return result.get();
//System.out.println("Your name: " + result.get());
}
return null;
}
}
它工作正常,但我希望在打开此意图时收到通知(最终用户点击通知打开URL)。
我尝试使用OnFinished回调,但这样做:
private void sendNotification(String message) {
PendingIntent pendingIntent;
Intent intent;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (url != null) {
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url));
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
pendingIntent在收到通知后立即打开
我试过像这样使用BroadcastReceiver:
我的活动:
PendingIntent.OnFinished finish = new PendingIntent.OnFinished() {
public void onSendFinished(PendingIntent pi, Intent intent,
int resultCode, String resultData, Bundle resultExtras) {
//sending information to Ordolink server as user opened the URL
...do my stuff....
}
};
try {
pendingIntent.send(Activity.RESULT_OK, finish, null);
} catch (CanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BroadcastReceiver:
Intent intent_broadcast = new Intent(this, MyBroadcastReceiver.class);
intent_broadcast.putExtra("url", url);
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent_broadcast, PendingIntent.FLAG_ONE_SHOT);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent_broadcast,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
}
我还在Manifest中声明了BroadcastReceiver:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("JRE", "Callback onReceive Intent open URL");
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.setData(Uri.parse(intent.getStringExtra("url")));
context.startActivity(myIntent);
}
但永远不会调用MyBroadcastReceiver。任何想法?
答案 0 :(得分:1)
我建议您在应用中使用BroadcastReceiver
Intent
作为PendingIntent
BroadcastReceiver
打开网址,PendingIntent pendingIntent;
Intent intent;
NotificationManager notificationManager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
if (url != null) {
intent = new Intent(context, MyBroadcastReceiver.class);
intent.putExtra("url", url);
pendingIntent = PendingIntent.getBroadcast(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
}
您可以知道何时用户打开了通知。
示例代码 -
BroadcastReceiver
在public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
/* Whatever you want to do */
...
/* Finally open the URL */
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.setData(Uri.parse(intent.getStrinExtra("url")));
context.startActivity(myIntent);
}
}
-
RequestResult strReq = OAuthUtility.Get
(
"https://api.dropboxapi.com/1/copy_ref/auto",
new HttpParameterCollection
{
{"access_token", "AccessToken"},
{"path", Path.Combine("CurrentPath", "FileName").Replace("\\", "/")}
}
);