我是android开发的新手。
我创建了一个用于接收pushNotification的后台服务(使用我们自己的套接字服务,而不是FCM)。 现在我的设备收到消息并成功显示通知。但是,当我单击通知时,它不会返回我的应用程序。
当我单击通知时,显示以下错误: ActivityManager:无法启动服务意图{cmp = com.my.app / package.name.AppActivity} U = 0:找不到
这是我的代码:
public static void setNotification(Service service, String message) {
NotificationManager manager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE);
String appName = service.getString(R.string.app_name);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(service)
.setContentTitle(appName)
.setContentText(message)
.setContentIntent(getDefaultIntent(service))
.setWhen(System.currentTimeMillis())
.setOngoing(false)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(service.getResources(), R.drawable.icon))
.setSound(alarmSound);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(1, notification);
}
private static PendingIntent getDefaultIntent(Service service) {
Intent intent = new Intent(service, AppActivity.class);
PendingIntent pendingIntent = PendingIntent.getService(service, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
return pendingIntent;
}
}
AndroidManifest.xml中的MainActivity设置
<activity android:name="package.name.AppActivity" android:configChanges="orientation|screenSize" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="landscape" android:theme= "@android:style/Theme.NoTitleBar.Fullscreen" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
请指导我这段代码中我所缺少或做错的事情。非常感谢。
答案 0 :(得分:1)
我在填充意图和manager.notify(0,notification);
中传递了相同的ID并使用PendingIntent.getActivity而不是PendingIntent.getService
有效!
答案 1 :(得分:0)
我遇到的问题是,您仅取消了当前通知,这就是为什么Intent无法触发活动更改您的Pending Intent
来自
componentDidMount() {
fetch('https://facebook.github.io/react-native/movies.json')
.then(response => response.json())
.then(responseJson => {
//Successful response from the API Call
this.setState({
serverData: [...this.state.serverData, ...responseJson.movies],
//adding the new data in Data Source of the SearchableDropdown
});
})
.catch(error => {
console.error(error);
});
}
render() {
return (
<View style={{ flex: 1, marginTop: 30 }}>
<Text style={{ marginLeft: 10 }}>
Searchable Dropdown from Dynamic Array from Server
</Text>
<SearchableDropdown
setSort={(item, searchedText)=> item.title.toLowerCase().startsWith(searchedText.toLowerCase())}
onTextChange={text => console.log(text)}
//On text change listner on the searchable input
onItemSelect={item => alert(JSON.stringify(item))}
//onItemSelect called after the selection from the dropdown
containerStyle={{ padding: 5 }}
//suggestion container style
textInputStyle={{
//inserted text style
padding: 12,
borderWidth: 1,
borderColor: '#ccc',
backgroundColor: '#FAF7F6',
}}
itemStyle={{
//single dropdown item style
padding: 10,
marginTop: 2,
backgroundColor: '#FAF9F8',
borderColor: '#bbb',
borderWidth: 1,
}}
itemTextStyle={{
//single dropdown item's text style
color: '#222',
}}
itemsContainerStyle={{
//items container style you can pass maxHeight
//to restrict the items dropdown hieght
maxHeight: '50%',
}}
items={this.state.serverData}
//mapping of item array
defaultIndex={2}
//default selected item index
placeholder="placeholder"
//place holder for the search input
resetValue={false}
//reset textInput Value with true and false state
underlineColorAndroid="transparent"
//To remove the underline from the android input
/>
</View>
);
}
到
PendingIntent.FLAG_CANCEL_CURRENT);