好的,我现在正在做的是通过FCM获得推送通知,这一过程进展顺利。现在,我可以在应用程序处于前台时更改活动,但是当我点击通知面板中的通知时,如何更改它?需要帮助。
我的代码:
public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {
// Check for empty push message
if (TextUtils.isEmpty(message))
return;
// notification icon
final int icon = R.mipmap.ic_launcher;
// on click activity for the notification !!!!!!!!!!
intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(mContext, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
答案 0 :(得分:5)
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long notificatioId = System.currentTimeMillis();
Intent intent = new Intent(getApplicationContext(), TestActivity.class); // Here pass your activity where you want to redirect.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, 0);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
currentapiVersion = R.mipmap.ic_notification_lolipop;
} else{
currentapiVersion = R.mipmap.ic_launcher;
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(currentapiVersion)
.setContentTitle(this.getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent);
mNotificationManager.notify((int) notificatioId, notificationBuilder.build());
}
答案 1 :(得分:1)
我以这种方式开始了一项特定的活动:
FireBaseMessagingService.java
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//The message will contain the Push Message
String message = remoteMessage.getData().get("message");
//imageUri will contain URL of the image to be displayed with Notification
String imageUri = remoteMessage.getData().get("image");
//title for the notification.
String title = remoteMessage.getData().get("title");
//action string to perform the action e.g. open activity
String action = remoteMessage.getData().get("click_action");
//To get a Bitmap image from the URL received
bitmap = getBitmapfromUrl(imageUri);
//method for functioning the notification --->
sendNotification(message, title, bitmap, action);
}
private void sendNotification(String messageBody, String title, Bitmap image, String action) {
Intent intent = new Intent(this, SpecificActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title", title);
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 50, _bs);
intent.putExtra("img", image);
intent.putExtra("msg", messageBody);
intent.putExtra("click_action", action);
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, "Default")
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.mipmap.app_icon)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_HIGH)
.setChannelId("Default")
.setVibrate(new long[]{1000, 1000})
.setContentIntent(pendingIntent);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(messageBody);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
此后,在 AndroidManifest.xml 文件的specificActivity.java部分中添加以下行:
<activity
android:name=".SpecificActivity">
<intent-filter>
<action android:name="OPEN_ACTIVITY" />
<!-- Add this OPEN_ACTIVITY string into your data payload while sending the notification from server side. -->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
在此之后,您便开始了特定活动的意图,即 SpecificActivity.java 文件的onCreate()方法。
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet())
{
String value = getIntent().getExtras().getString(key);
if (key.equals("click_action")) {
//perform the action you want to do with the key.
}
添加这些内容后,您可以检查来自移动端的通知。
答案 2 :(得分:0)
Pass your Activity you want to open when clicked into Intent.
Intent notificationIntent = new Intent(context, XYZActivity.class);
complete code..
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, XYZActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
答案 3 :(得分:0)
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(mContext,ACTIVITY_TO_BE_DISPLAYED.class); // Replace ACTIVITY_TO_BE_DISPLAYED to Activity to which you wanna show
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
.setAutoCancel(true)
.setTicker("YOUR_TICKER_MSG")
.setSmallIcon(R.drawable.ic_notification_icon)
.setLargeIcon(icon)
.setContentTitle("YOUR_TITLE")
.setContentText("YOUR_TEXT")
.setContentIntent(intent);
notificationManager.notify(10, builder.build());
答案 4 :(得分:0)
<!-- MainActivity is the parent for ResultActivity -->
<activity
android:name=".ResultActivity"
/>
不要忘记用子活动声明来调整清单