繁体   English   中英

Android通知意图未启动所述活动和通知声音问题

[英]Android Notification Intent Not launching the said Activity and Notification Sound Issue

嗨,我正在使用Firebase Cloud Messaging的应用程序上工作,并且我尝试在应用程序使用Firebase Cloud Messaging接收消息时显示通知。
当应用程序运行时

  1. 我可以听到通知声
  2. 当我点击通知时,在我的情况下,它使我进入正确的活动NotificationActivity.java

现在,当关闭App时,它不会在后台运行(这些主要问题需要解决,并且需要针对这种不自然的行为进行解释)

  1. 我可以看到通知,但听不到通知声音。
  2. 当我点击通知时,它不会打开NotificationActivity.java而是打开Splash.java ,这是默认/启动器活动

以下是代码片段。

Mainfest.xml

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Splash"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            android:windowSoftInputMode="stateHidden" />
        <activity
            android:name=".SignupActivity"
            android:windowSoftInputMode="stateHidden" />
        <activity
            android:name=".Dashboard"
            android:label="@string/title_activity_dashboard" />

        <activity android:name="pk.edu.kiu.jobslocatoer.NotificationActivity"
            android:launchMode="singleTask"
            android:taskAffinity=""
            android:excludeFromRecents="true">
            <intent-filter>
                <action android:name="pk.edu.kiu.jobslocatoer.MESSAGING_ACTIVTY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <receiver android:name=".receivers.NotificationReceiver">
            <intent-filter>
                <action android:name="pk.edu.kiu.jobslocatoer.MESSAGING_EVENT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
        <activity android:name=".SettingsActivity"></activity>

        <service android:name=".services.MessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <service android:name=".services.InstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>


    </application>

NotificationReceiver.java

public class NotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent notify = new Intent(context, NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) SystemClock.currentThreadTimeMillis(), notify, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new Notification.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Titile")
                .setContentText("Text")
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        notificationManager.notify((int) SystemClock.currentThreadTimeMillis(), notification);
    }
}

MessagingService.java

public class MessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(GlobalConfig.TAG, GlobalConfig.getRef(this) + " notificaiton: " + remoteMessage.getNotification().getBody());
        sendBroadcast(new Intent("pk.edu.kiu.jobslocatoer.MESSAGING_EVENT"));
    }
}
Use this code:    
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
           NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
           if (bitmaplogo != null) {
               notificationBuilder.setLargeIcon(bitmaplogo);
           } else {
               notificationBuilder.setLargeIcon(largeIcon);
           }
           if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
               notificationBuilder.setSmallIcon(R.drawable.app_icon_transparent);
           } else {
               notificationBuilder.setSmallIcon(R.mipmap.app_icon);
           }
           notificationBuilder.setColor(getResources().getColor(R.color.colorAccent));
           notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
           notificationBuilder.setContentText(messageBody);
           notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody));
           notificationBuilder.setAutoCancel(true);
           notificationBuilder.setSound(defaultSoundUri);
           notificationBuilder.setContentIntent(pendingIntent);

           int num = (int) System.currentTimeMillis();
           NotificationManager notificationManager =
                   (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
           notificationManager.notify(num, notificationBuilder.build());
 Intent resultIntent = null;

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(context.getString(R.string.app_name))
                        .setAutoCancel(true)
                        .setContentText(message);




        resultIntent = new Intent(context,Activity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(Activity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        5,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(pushId, mBuilder.build());

尝试这个:

 Intent intent = new Intent(this, NotificationActivity.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.ic_stat_name)
            .setContentTitle("Title")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

Firebase通知

1.如果您是通过Firebase控制台发送通知,firebase会像这样向您的消息中添加一个通知对象

{
   //..
    "notification" : {
      "body" : "message body sample text",
      "title" : "notification Tittle",
      "icon" : "icon information sample"
    }
   //..
  }

1)如果您的通知包含此通知有效负载,并且如果您的应用程序处于后台

  • 通知将发送到设备的系统托盘。 用户点击通知会默认打开应用启动器。

2)如果您的通知包含此通知有效负载 + 数据有效负载,并且如果您的应用程序处于后台

  • 通知将传递到设备的系统托盘,数据有效载荷将在启动器活动的意图范围内传递。

3)如果您的通知仅包含数据有效载荷{data" : {"body" : "message body sample text","title" : "notification Tittle","icon" : "icon information sample"} ),并且您的应用程序然后在背景前景中

  • Firebase消息处理程序的OnMessageReceived()方法被调用,我们可以编写代码来显示此方法中的通知。

因此,如果您想在每次收到通知时都执行OnMessageReceived()方法,则应始终从服务器端传递仅数据有效负载。否则, OnMessageReceived()将不会执行,Firebase将处理显示事物的通知。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM