Last edited:
You should leave it checked. If it is not checked then AndroidManifest.xml is recreated by the compiler.Unchecked 'Do Not Overwrite Manifest File' (It was complaining about this during compilation)
You should leave it checked. If it is not checked then AndroidManifest.xml is recreated by the compiler.
You will need to redo the changes and keep it checked.
Can you upload your project (File - Export as zip)?
You didn't change the Package name (under Project):
Hi Erel, thanks for the reply. I want to implement C2DM functionality into my Project. So I have created a new service module in my project and copy pasted the code from this tutorial's service module into my service module. Now, what do I do in my manifest file?Not sure which answer are you looking for. Which problem are you encountering?
You should then choose Project - Do not overwrite manifest file.You should now edit AndroidManifest.xml which is located under Objects folder and replace all occurrences of anywheresoftware.b4a.samples.push with your package name.
this is my projects manifest file. I have changed the manifest file and have ticked, Do not overwrite manifest file.As mentioned in the first post:
You should then choose Project - Do not overwrite manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.home.test.push"
android:versionCode="1"
android:versionName=""
android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<permission android:name="com.home.test.push.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.home.test.push.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:icon="@drawable/icon"
android:label="PushTest">
<activity
android:windowSoftInputMode="stateHidden"
android:launchMode="singleTop"
android:name=".main"
android:label="PushTest"
android:screenOrientation="unspecified">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".push">
</service>
<receiver android:name=".push$push_BR" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.home.test.push" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.home.test.push" />
</intent-filter>
</receiver>
</application>
</manifest>
this is my projects manifest file. I have changed the manifest file and have ticked, Do not overwrite manifest file.
B4X:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.home.test.push" android:versionCode="1" android:versionName="" android:installLocation="internalOnly"> <uses-sdk android:minSdkVersion="4" /> <supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:anyDensity="true"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.VIBRATE"/> <permission android:name="com.home.test.push.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.home.test.push.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <application android:icon="@drawable/icon" android:label="PushTest"> <activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name=".main" android:label="PushTest" android:screenOrientation="unspecified"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".push"> </service> <receiver android:name=".push$push_BR" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.home.test.push" /> </intent-filter> <!-- Receive the registration id --> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.home.test.push" /> </intent-filter> </receiver> </application> </manifest>
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Package, DeviceBoardPassword, BoardUrl, SenderId, DeviceName As String
DeviceBoardPassword = "abcd"
BoardUrl = "http://ssahmed.co.uk/c2dm/c2dm_board.php"
'both these fields should be set to match your application package and SenderId.
Package = "com.home.test.push"
SenderId = "my_email@gmail.com"
Dim reg As String
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim p As PhoneId
Dim ListView1 As ListView
Dim Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
reg = p.GetDeviceId
Log("The device ID is: " & reg)
'ToastMessageShow("Starting Service", False)
Register(reg)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Register(Name As String)
Dim i As Intent
i.Initialize("", "")
i.SetComponent(Package & "/.push")
i.PutExtra("MyRequest", "Register")
DeviceName = Name
ToastMessageShow("Register,Main: " & Name, False)
Log("Service name: " & i)
StartService(i)
End Sub
Sub Unregister(Name As String)
Dim i As Intent
i.Initialize("", "")
i.SetComponent(Package & "/.push")
i.PutExtra("MyRequest", "Unregister")
DeviceName = Name
ToastMessageShow("Un-Register,Main: " & Name, False)
StartService(i)
End Sub
'Service module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim hc As HttpClient
Dim hcInit As Boolean
hcInit = False
Dim RegisterTask, UnregisterTask As Int
RegisterTask = 1
UnregisterTask = 2
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
ToastMessageShow("Service started", False)
Log("servicestarted")
If StartingIntent.HasExtra("MyRequest") Then
Select StartingIntent.GetExtra("MyRequest")
Case "Register"
RegisterDevice(False)
Case "Unregister"
RegisterDevice(True)
End Select
Else
Select StartingIntent.Action
Case "com.google.android.c2dm.intent.REGISTRATION"
HandleRegistrationResult(StartingIntent)
Case "com.google.android.c2dm.intent.RECEIVE"
MessageArrived(StartingIntent)
End Select
End If
End Sub
Sub Service_Destroy
End Sub
Sub MessageArrived (Intent As Intent)
Dim From, CollapseKey, Data As String
If Intent.HasExtra("from") Then From = Intent.GetExtra("from")
If Intent.HasExtra("data") Then Data = Intent.GetExtra("data")
If Intent.HasExtra("collapse_key") Then CollapseKey = Intent.GetExtra("collapse_key")
'Here you should handle the new message:
Log("New message arrived: " & Data)
ToastMessageShow("New message: " & Data, True)
'Msgbox(Data, "New Message")
Dim n As Notification
n.Initialize
'n.AutoCancel = True
n.SetInfo("New Message", Data, "")
'n.Vibrate = False
n.Notify(1)
End Sub
Sub RegisterDevice (Unregister As Boolean)
Dim i As Intent
If Unregister Then
i.Initialize("com.google.android.c2dm.intent.UNREGISTER", "")
Else
i.Initialize("com.google.android.c2dm.intent.REGISTER", "")
i.PutExtra("sender", Main.SenderId)
End If
Dim r As Reflector
Dim i2 As Intent
i2 = r.CreateObject("android.content.Intent")
Dim pi As Object
pi = r.RunStaticMethod("android.app.PendingIntent", "getBroadcast", _
Array As Object(r.GetContext, 0, i2, 0), _
Array As String("android.content.Context", "java.lang.int", "android.content.Intent", "java.lang.int"))
i.PutExtra("app", pi)
StartService(i)
End Sub
Sub HandleRegistrationResult(Intent As Intent)
If Intent.HasExtra("error") Then
Log("Error: " & Intent.GetExtra("error"))
ToastMessageShow("Error: " & Intent.GetExtra("error"), True)
Else If Intent.HasExtra("unregistered") Then
If hcInit = False Then hc.Initialize("hc")
Dim req As HttpRequest
req.InitializeGet(Main.BoardUrl & "?device_password=" & Main.DeviceBoardPassword & _
"&name=" & Main.DeviceName & "&id=") 'Empty id is sent here. This will cause the board to delete this name.
hc.Execute(req, UnregisterTask)
Else If Intent.HasExtra("registration_id") Then
If hcInit = False Then hc.Initialize("hc")
Dim rid As String
rid = Intent.GetExtra("registration_id")
Dim req As HttpRequest
req.InitializeGet(Main.BoardUrl & "?device_password=" & Main.DeviceBoardPassword & _
"&name=" & Main.DeviceName & "&id=" & rid)
hc.Execute(req, RegisterTask)
End If
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Select TaskId
Case RegisterTask
ToastMessageShow("Registration completed successfully.", False)
Case UnregisterTask
ToastMessageShow("Unregistration completed successfully.", False)
End Select
Response.Release
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Dim errorMsg As String
errorMsg = "Code=" & StatusCode & ", " & Reason
If Response <> Null Then
errorMsg = errorMsg & ", " & Response.GetString("UTF8")
Response.Release
End If
ToastMessageShow(errorMsg, True)
Log(errorMsg)
End Sub