簡體   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