所以,我通过网络搜索,并没有看到任何人遇到我遇到的这个问题。
我正在构建一个需要推送通知的应用程序。推送通知显示在状态栏和锁定屏幕上,但仅在应用程序处于活动状态时发出声音。我不知道我错过了什么。
这是我的代码:
public class PushListenerService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
Log.d("FROM", from);
Bundle pushNotification = (Bundle) data.get("notification");
String title = pushNotification.get("title").toString();
String body = pushNotification.get("body").toString();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder notification = new Notification.Builder(this);
notification.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.portrait_front)
.setSound(alarmSound);
notificationManager.notify(0, notification.build());
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
编辑:添加清单以供进一步检查:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sth.idapp.android.privateapp">
<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:largeHeap="true"
android:theme="@style/AppTheme">
<activity
android:screenOrientation="portrait"
android:name=".start.StartActivity"
android:theme="@style/AppTheme.HideActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> …Run Code Online (Sandbox Code Playgroud)