在通知上打开特定活动在Android中单击

时间:2018-02-15 04:56:41

标签: android firebase firebase-cloud-messaging

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

    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);
    }
}