在 Android 中单击通知时打开特定活动
Posted
技术标签:
【中文标题】在 Android 中单击通知时打开特定活动【英文标题】:Open specific Activity on Notifications Click in Android 【发布时间】:2018-02-15 04:56:40 【问题描述】:我正在使用 firebase 消息服务收到通知。一切正常,但我面临的一个问题是单击通知时我无法打开通知详细信息活动。虽然调试它工作正常,但在发布版本中,我无法找到错误。
这是我的代码:
Log.d(TAG, "From: " + remoteMessage.getFrom());
NewsId = remoteMessage.getNotification().getIcon();
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent i = new Intent(this, NotificationDetailActivity.class);
i.putExtra("NewsId", NewsId);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(html.fromHtml(remoteMessage.getNotification().getTitle()))
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT))
.setSound(soundUri);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setColor(getResources().getColor(R.color.colorAccent));
else
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
Notification mNotification = mBuilder.build();
mNotification.flags |= Notification.FLAG_INSISTENT;
// Display notification
notificationManager.notify(100, mNotification);
【问题讨论】:
添加到意图标志:notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 你在用proguard吗? @MohdSaquib 你能解释一下吗? @MasoudMokhtari No 看看这可能对你有帮助:***.com/questions/40417127/… 【参考方案1】:是的,您可以通过单击通知打开活动,这也是 FCM 示例消息正文来自 FCM
sendNewsNotify(remoteMessage.getData().get("message"));
private void sendNotification(String messageBody, String id)
long when = System.currentTimeMillis();
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
Intent intent = new Intent(this, ActivityNotification.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title", messageBody);
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getActivity(this, /*(int) when*/0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.logo)
.setLargeIcon(largeIcon)
.setContentTitle("news")
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(/*(int) when*/0, notificationBuilder.build());
【讨论】:
@NeerajMehta 您将使用挂起的意图,并且您没有设置任何标志来清除顶部或开始新任务... @MohdSaquib 好的,让我看看这段代码是否适合我 @MohdSaquib 当我使用调试点对其进行调试时,它工作正常这有什么问题......我必须在清单中做任何事情来定义这个活动 你的应用在后台还是前台 @MohdSaquib 它基本上是一个新闻应用程序,只有这个通知服务是后台服务【参考方案2】: private void sendNotification(String msg, String theTitle)
Context mContext = getApplicationContext();
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> services = activityManager
.getRunningTasks(Integer.MAX_VALUE);
boolean isActivityFound = false;
if (services.get(0).topActivity.getPackageName().toString()
.equalsIgnoreCase(getPackageName().toString()))
isActivityFound = true;
Intent openIntent = null;
if (isActivityFound)
openIntent = new Intent();
else
openIntent = new Intent(this, MainActivity.class);
openIntent.putExtra("Title", the_title);
openIntent.putExtra("Message", the_message);
openIntent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
openIntent, PendingIntent.FLAG_ONE_SHOT);
if (msg != null && (!msg.isEmpty()))
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setDefaults(Notification.DEFAULT_ALL)
.setVibrate(new long[]100, 250, 100, 250, 100, 250)
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.activity_toolbar_color))
.setContentTitle(theTitle)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Html.fromHtml(msg)))
.setPriority(Notification.PRIORITY_MAX)
.setContentText(Html.fromHtml(msg));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
mBuilder.setSmallIcon(R.drawable.notification_icon1);
else
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentIntent(contentIntent);
SharedPreferences prefs = mContext.getSharedPreferences(MainActivity.class.getSimpleName(), Context.MODE_PRIVATE);
int notificationNumber = prefs.getInt("notificationNumber", 0);
mNotificationManager.notify(notificationNumber, mBuilder.build());
SharedPreferences.Editor editor = prefs.edit();
if (notificationNumber < 5000)
notificationNumber++;
else
notificationNumber = 0;
editor.putInt("notificationNumber", notificationNumber);
editor.commit();
Log.d("notificationNumber", "" + notificationNumber);
这有助于在手机启动时在后台启动 FCM 服务。
Add receiver in Manifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name=".OnBootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
OnBootBroadcastReceiver.class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OnBootBroadcastReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
Intent i = new Intent("com.examle.FirebaseMessagingReceiveService");
i.setClass(context, FirebaseMessagingReceiveService.class);
context.startService(i);
【讨论】:
这个方法需要在fcm的onMessageReceived方法里面调用。 是的,我只在那儿打电话 我无法打开通知。 ..这是因为我正在调用的启动屏幕..当我点击通知应用程序以启动屏幕开始并且在启动活动中我在 1 秒后定义它将打开 mainactivity 那么现在这个怎么处理呢? 点击通知调用你想打开的活动,就像我做的那样(MainActivity.class)以上是关于在 Android 中单击通知时打开特定活动的主要内容,如果未能解决你的问题,请参考以下文章
在应用关闭时单击 Firebase 通知后打开特定的活动/片段
当应用程序不在前台时,特定活动不会从通知单击打开(应用程序是最近的,没有被杀死!)