嗨我正在使用cordova构建应用程序但是fadeup来抓住这个问题任何人都知道什么是确切的问题,我还要检查每个n的每个东西n android manifest.xml如果有人面对这个问题,请提前感谢我。< / p>
我也使用了正确的项目编号。
无法启动服务Intent {act = com.google.android.c2dm.intent.REGISTER pkg = com.google.android.gsf(有额外内容)} U = 0:找不到但是给出了
Controller.js
/**
* Author: hollyschinsky
* twitter: @devgirfl
* blog: devgirl.org
* more tutorials: hollyschinsky.github.io
*/
app.controller('AppCtrl', function($scope, $cordovaPush, $cordovaDialogs, $cordovaMedia, $cordovaToast, ionPlatform, $http) {
$scope.notifications = [];
// call to register automatically upon device ready
ionPlatform.ready.then(function (device) {
$scope.register();
});
// Register
$scope.register = function () {
var config = null;
if (ionic.Platform.isAndroid()) {
config = {
"senderID": "XXXXXXXXXXXXX",
"ecb":"onNotificationGCM"
// project URL like: https://console.developers.google.com/project/434205989073
};
}
else if (ionic.Platform.isIOS()) {
config = {
"badge": "true",
"sound": "true",
"alert": "true",
"ecb":"onNotificationAPN"
}
}
$cordovaPush.register(config).then(function (result) {
alert(JSON.stringify(result))
console.log("Register success " + result);
$cordovaToast.showShortCenter('Registered for push notifications');
$scope.registerDisabled=true;
// ** NOTE: Android regid result comes back in the pushNotificationReceived, only iOS returned here
if (ionic.Platform.isIOS()) {
$scope.regId = result;
storeDeviceToken("ios");
}
}, function (err) {
console.log("Register error " + err)
});
}
// Notification Received
$scope.$on('$cordovaPush:notificationReceived', function (event, notification) {
console.log(JSON.stringify([notification]));
if (ionic.Platform.isAndroid()) {
handleAndroid(notification);
}
else if (ionic.Platform.isIOS()) {
handleIOS(notification);
$scope.$apply(function () {
$scope.notifications.push(JSON.stringify(notification.alert));
})
}
});
// Android Notification Received Handler
function handleAndroid(notification) {
// ** NOTE: ** You could add code for when app is in foreground or not, or coming from coldstart here too
// via the console fields as shown.
console.log("In foreground " + notification.foreground + " Coldstart " + notification.coldstart);
if (notification.event == "registered") {
$scope.regId = notification.regid;
storeDeviceToken("android");
}
else if (notification.event == "message") {
$cordovaDialogs.alert(notification.message, "Push Notification Received");
$scope.$apply(function () {
$scope.notifications.push(JSON.stringify(notification.message));
})
}
else if (notification.event == "error")
$cordovaDialogs.alert(notification.msg, "Push notification error event");
else $cordovaDialogs.alert(notification.event, "Push notification handler - Unprocessed Event");
}
// IOS Notification Received Handler
function handleIOS(notification) {
// The app was already open but we'll still show the alert and sound the tone received this way. If you didn't check
// for foreground here it would make a sound twice, once when received in background and upon opening it from clicking
// the notification when this code runs (weird).
if (notification.foreground == "1") {
// Play custom audio if a sound specified.
if (notification.sound) {
var mediaSrc = $cordovaMedia.newMedia(notification.sound);
mediaSrc.promise.then($cordovaMedia.play(mediaSrc.media));
}
if (notification.body && notification.messageFrom) {
$cordovaDialogs.alert(notification.body, notification.messageFrom);
}
else $cordovaDialogs.alert(notification.alert, "Push Notification Received");
if (notification.badge) {
$cordovaPush.setBadgeNumber(notification.badge).then(function (result) {
console.log("Set badge success " + result)
}, function (err) {
console.log("Set badge error " + err)
});
}
}
// Otherwise it was received in the background and reopened from the push notification. Badge is automatically cleared
// in this case. You probably wouldn't be displaying anything at this point, this is here to show that you can process
// the data in this situation.
else {
if (notification.body && notification.messageFrom) {
$cordovaDialogs.alert(notification.body, "(RECEIVED WHEN APP IN BACKGROUND) " + notification.messageFrom);
}
else $cordovaDialogs.alert(notification.alert, "(RECEIVED WHEN APP IN BACKGROUND) Push Notification Received");
}
}
// Stores the device token in a db using node-pushserver (running locally in this case)
//
// type: Platform type (ios, android etc)
function storeDeviceToken(type) {
// Create a random userid to store with it
var user = { user: 'user' + Math.floor((Math.random() * 10000000) + 1), type: type, token: $scope.regId };
console.log("Post token for registered device with data " + JSON.stringify(user));
alert(JSON.stringify(user));
//$http.post('http://192.168.1.16:8000/subscribe', JSON.stringify(user))
// .success(function (data, status) {
// console.log("Token stored, device is successfully subscribed to receive push notifications.");
// })
// .error(function (data, status) {
// console.log("Error storing device token." + data + " " + status)
// }
//);
}
// Removes the device token from the db via node-pushserver API unsubscribe (running locally in this case).
// If you registered the same device with different userids, *ALL* will be removed. (It's recommended to register each
// time the app opens which this currently does. However in many cases you will always receive the same device token as
// previously so multiple userids will be created with the same token unless you add code to check).
function removeDeviceToken() {
var tkn = {"token": $scope.regId};
//$http.post('http://192.168.1.16:8000/unsubscribe', JSON.stringify(tkn))
// .success(function (data, status) {
// console.log("Token removed, device is successfully unsubscribed and will not receive push notifications.");
// })
// .error(function (data, status) {
// console.log("Error removing device token." + data + " " + status)
// }
//);
}
// Unregister - Unregister your device token from APNS or GCM
// Not recommended: See http://developer.android.com/google/gcm/adv.html#unreg-why
// and https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/unregisterForRemoteNotifications
//
// ** Instead, just remove the device token from your db and stop sending notifications **
$scope.unregister = function () {
console.log("Unregister called");
removeDeviceToken();
$scope.registerDisabled=false;
//need to define options here, not sure what that needs to be but this is not recommended anyway
// $cordovaPush.unregister(options).then(function(result) {
// console.log("Unregister success " + result);//
// }, function(err) {
// console.log("Unregister error " + err)
// });
}
})
AndroidManifest.xml
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="org.devgirl.push.sample" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:exported="true" android:name="com.plugin.gcm.PushHandlerActivity" />
<receiver android:name="com.plugin.gcm.CordovaGCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="org.devgirl.push.sample" />
</intent-filter>
</receiver>
<service android:name="com.plugin.gcm.GCMIntentService" />
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="org.devgirl.push.sample.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="org.devgirl.push.sample.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>