我看到这不是一个不常见的主题,我试图阅读其他帖子,但我不知道如何将其应用到我的应用程序。在我的应用程序中,我通过PostMan发送推送通知,然后根据我设置的标志打开不同的活动。我想发送不同的通知链接,所以当用户点击它时,它会打开一个浏览器。换句话说,我希望在通知专用于活动时同时拥有活动标志,并在通知包含特定链接时使用URL。
我从previous post的答案中看到,一个解决方案就是这样设定的意图:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("http://www.google.com"));
PendingIntent contentIntent = PendingIntent.getActivity(contexta, 0, notificationIntent, 0);
notification.setLatestEventInfo(contexta, contentTitle, contentText, contentIntent);
mNotificationManager.notify(970970, notification);
但据我所知,默认设置为www.google.com。我怎样才能打开PostMan发送的特定链接?我假设我需要以某种方式将上述代码引入我的IF条件(如在here中),但是如何?
我对PushNotifications的代码如下:
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMessagingServic";
public MyFirebaseMessagingService() {}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
try {
JSONObject data = new JSONObject(remoteMessage.getData());
String jsonMessage = data.getString("extra_information");
Log.d(TAG, "onMessageReceived: \n" +
"Extra Information: " + jsonMessage);
} catch (JSONException e) {
e.printStackTrace();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle(); //get title
String message = remoteMessage.getNotification().getBody(); //get message
String click_action = remoteMessage.getNotification().getClickAction(); //get click_action
Log.d(TAG, "Message Notification Title: " + title);
Log.d(TAG, "Message Notification Body: " + message);
Log.d(TAG, "Message Notification click_action: " + click_action);
sendNotification(title, message, click_action);
}
}
@Override
public void onDeletedMessages() {
}
private void sendNotification(String title, String messageBody, String click_action) {
Intent intent;
if (click_action.equals("EVENIMENTE")) {
intent = new Intent(this, Evenimente.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else if (click_action.equals("PRIMA_PAGINA")) {
intent = new Intent(this, MainScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else {
intent = new Intent(this, MainScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */ , intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_logo_final)
.setContentTitle("App")
.setContentText(messageBody)
.setAutoCancel(true)
.setLights(Color.RED, 3000, 3000)
.setSound(defaultSoundUri)
.setVibrate(new long[] {
1000,
1000,
1000,
1000,
1000
})
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */ , notificationBuilder.build());
}
}
所以,我有click_action
设置,正如其他帖子中也提到的那样,所以我猜这是从PostMan发送链接的地方,但是如何?有人可以帮我这个吗?谢谢!
答案 0 :(得分:1)
让我解释几件事。意图可以分配给他们的动作,活动可以有意图过滤器。意图过滤器就像是说,无论何时系统广播这种意图,我都是一个活动,我愿意处理它。这些intent过滤器写在清单文件中。所以你的活动看起来像这样。这种意图称为隐含意图。
<activity
android:name=".MainActivity" >
<intent-filter>
<action android:name="com.example.MY_ACTION" />
</intent-filter>
</activity>
当我想从一个隐含的意图开始这个活动时,它将以这种方式完成。
Intent intent = new Intent("com.example.MY_ACTION");
现在回答您的问题,您可以改进代码中的某些内容。您可以在Manifest文件中的活动中包含intent过滤器。
因此,您不必放置if
条件。
就像您的自定义意图操作一样,浏览器的默认操作为Intent.ACTION_VIEW
。现在,您只需从邮递员的邮件中发送正确的操作,为这些操作创建隐式意图,您就完成了。
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMessagingServic";
public MyFirebaseMessagingService() {}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
try {
JSONObject data = new JSONObject(remoteMessage.getData());
String jsonMessage = data.getString("extra_information");
Log.d(TAG, "onMessageReceived: \n" +
"Extra Information: " + jsonMessage);
} catch (JSONException e) {
e.printStackTrace();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle(); //get title
String message = remoteMessage.getNotification().getBody(); //get message
String click_action = remoteMessage.getNotification().getClickAction(); //get click_action
String uri = remoteMessage.getNotification().getLink(); //parsing url
Log.d(TAG, "Message Notification Title: " + title);
Log.d(TAG, "Message Notification Body: " + message);
Log.d(TAG, "Message Notification click_action: " + click_action);
Log.d(TAG, "Message Notification uri: " + uri);
sendNotification(title, message, click_action, uri);
}
}
@Override
public void onDeletedMessages() {
}
private void sendNotification(String title, String messageBody, String click_action, Uri uri) {
Intent intent;
/*if (click_action.equals("EVENIMENTE")) {
intent = new Intent(this, Evenimente.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else if (click_action.equals("PRIMA_PAGINA")) {
intent = new Intent(this, MainScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else {
intent = new Intent(this, MainScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}*/
//If conditions can be replaced with this. Not sure why wrote last else condition. If you want to start MainScreenActivity when there is no action or the action is PRIMA_PAGINA, you could simply write direct else.
if(uri != null) {
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
} else {
intent = new Intent(click_action);
//click_action will be "EVENIMENTE"/"com.example.EVENIMENTE" for Evenimente.class, PRIMA_PAGINA for MainScreenActivity.class
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */ , intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_logo_final)
.setContentTitle("App")
.setContentText(messageBody)
.setAutoCancel(true)
.setLights(Color.RED, 3000, 3000)
.setSound(defaultSoundUri)
.setVibrate(new long[] {
1000,
1000,
1000,
1000,
1000
})
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */ , notificationBuilder.build());
}
}
如上所述,为您的活动更新并添加intent-filter
。意图过滤器应该与您要在fcm消息中发送的click_action
完全相同。