打开有关通知的特定活动,请单击Android



我正在使用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);

是的,您可以通过单击通知打开活动,它也是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());
}
    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);
    }
}

相关内容

  • 没有找到相关文章

最新更新