Launch by Default

Put your problem here if it does not fit any of the other categories.

Launch by Default

Postby hasitharandika » Fri Mar 19, 2010 7:08 am

hi guys,

i have written a SMS application which will listen for incoming sms's and display to the user using my application. once sms is received notification will displayed too. if user selects the notification sms will displayed tot he user through native messaging application.

so is it possible once user selects the notification from which app does he wants to display the sms whether using my app or using the native app ??.

is it possible to display a customize Toast in the notifying area?? other than the native sms notification???

regards,
Randika
hasitharandika
Developer
Developer
 
Posts: 45
Joined: Mon Feb 15, 2010 8:37 am
Location: Sri Lanka

Top

Postby LStrike » Wed Mar 31, 2010 1:46 pm

You can use the "notification manager".

This ist the code of my sms application (sorry, for the german parts)


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void benachrichtige(String message){
  2.  
  3.                
  4.  
  5.         Context context = getApplicationContext();
  6.  
  7.        
  8.  
  9.         NotificationManager notificationManager =
  10.  
  11.           (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  12.  
  13.        
  14.  
  15.         String notificationText = "Neue Nachricht: "+ message;
  16.  
  17.        
  18.  
  19.         Notification notification = new Notification(R.drawable.padlock3, notificationText, System.currentTimeMillis());
  20.  
  21.        
  22.  
  23.         Intent activityIntent = new Intent(context, EmpfaengerGui.class);
  24.  
  25.         activityIntent.putExtra("SMS-MSG", message);
  26.  
  27.         activityIntent.putExtra("notificationNr", NOTIFICATION_ID);
  28.  
  29.         activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  30.  
  31.        
  32.  
  33.         PendingIntent startIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
  34.  
  35.        
  36.  
  37.         notification.setLatestEventInfo(context,"Neue Nachricht:", message, startIntent);
  38.  
  39.         notification.vibrate = new long[] {100,250};
  40.  
  41.        
  42.  
  43.         notificationManager.notify(NOTIFICATION_ID, notification);
  44.  
  45.        
  46.  
  47.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
User avatar
LStrike
Junior Developer
Junior Developer
 
Posts: 20
Joined: Mon Feb 22, 2010 2:22 pm
Location: Hamburg, Germany

Re: Launch by Default

Postby hasitharandika » Mon May 24, 2010 11:32 am

hi LStrike,

it works correctly, but found one issue which is how to get rid of the icon once message has been read.
after i read the message, the notification still appears in the notification bar.

regards,
Randika
hasitharandika
Developer
Developer
 
Posts: 45
Joined: Mon Feb 15, 2010 8:37 am
Location: Sri Lanka

Re: Launch by Default

Postby LStrike » Mon May 24, 2010 11:51 am

Hello Randika,

I only know how to delete my own notification.
This is the code:

Code: Select all
//delete Notification
        NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancelAll();


But I don´t know how to delete the Android SMS notification, which is set by the system.
FNORD this line does not exist!
-------------------------------------------------
T-Mobile G1 with Cyanogenmod 5.0.8 DS
HTC Desire S with Android 2.3.3
User avatar
LStrike
Junior Developer
Junior Developer
 
Posts: 20
Joined: Mon Feb 22, 2010 2:22 pm
Location: Hamburg, Germany

Re: Launch by Default

Postby hasitharandika » Mon May 24, 2010 12:43 pm

hi LStrike,

which is true, but we are displaying the NotificationManager from the BroadcastReceiver so how are you going to cancel it in another activity??

other thing is if you receive multiple messages how is this notification manager is going to behave???

from where have you define NOTIFICATION_ID??? is it a constants???

regards,
Randika
hasitharandika
Developer
Developer
 
Posts: 45
Joined: Mon Feb 15, 2010 8:37 am
Location: Sri Lanka

Re: Launch by Default

Postby LStrike » Mon May 24, 2010 1:22 pm

The NOTIFICATION_ID is a constant which you have to set.
Code: Select all
private static final int NOTIFICATION_ID =12345;


And now I have a problem ;)
I get only one notification, even if I get two ore more messages.
Only the first message is stored in this notification.

Here is the code where I set the notification (SetNotification)
Code: Select all
package smsEmpfaenger;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import bachelor.markus.R;;

public class SetNotification extends Activity{
   
   
   public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
       
        Bundle extras = getIntent().getExtras();
        if (extras != null){
           String msg = extras.getString("SMS-MSG");
           benachrichtige(msg);
        }
   }
   
   private static final int NOTIFICATION_ID =12345;
   
   public void benachrichtige(String message){
      
   Context context = getApplicationContext();
   
   NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   
   String notificationText = "Neue Nachricht: "+ message;
   
   Notification notification = new Notification(R.drawable.schloss, notificationText, System.currentTimeMillis());
   
   Intent activityIntent = new Intent(context, SmsEmpfaengerGUI.class);
   activityIntent.putExtra("SMS-MSG", message);
   activityIntent.putExtra("notificationNr", NOTIFICATION_ID);
   activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   
   PendingIntent startIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
   
   notification.setLatestEventInfo(context,"Neue Nachricht:", message, startIntent);
   notification.vibrate = new long[] {100,250};
   
   notificationManager.notify(NOTIFICATION_ID, notification);
   
   finish();
   }
}


And here is my Receiver GUI Code where I delete the notification:
Code: Select all
package smsEmpfaenger;

import smsSender.SendeSMS;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import bachelor.markus.R;;

public class SmsEmpfaengerGUI extends Activity{
   
   private EditText nachrichtFeld;
   private EditText passwortFeld;
   private Button entschluesselButton;
   private Button quitButton;
   
   private static final String TAG = "SmsEmpfaengerGUI";
   
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smsempfaenger);
       
        nachrichtFeld = (EditText) findViewById(R.id.empfaengerNachrichtFeld);
        passwortFeld = (EditText) findViewById(R.id.empfaengerPasswortFeld);
        entschluesselButton = (Button) findViewById(R.id.empfaengerEntschluelButton);
        quitButton = (Button) findViewById(R.id.empfaengerQuitButton);
       
        /**
         * "auspacken" des SMS-Body (eigentliche Nachricht)
         */
        Bundle extras = getIntent().getExtras();
        if (extras != null){
           String msg = extras.getString("SMS-MSG");
           nachrichtFeld.setText(msg);
        }
       
        quitButton.setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View v) {
            finish();
         }
      });
       
        entschluesselButton.setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View v) {
            String verschluesselt = nachrichtFeld.getText().toString();
            String passwort = passwortFeld.getText().toString();
            String klartext = "";
            if (verschluesselt.length() > 0 && passwort.length() > 0){
               try {
                  klartext = SendeSMS.entschluesselSMS(passwort, verschluesselt);
                  nachrichtFeld.setText(klartext);
               } catch (Exception e) {
                  // TODO Auto-generated catch block
                  nachrichtFeld.setError("Es ist ein Fehler aufgetreten");
                  Log.e(TAG, "Fehler beim Entschluesseln der SMS Nachricht: ", e);
               }
            }else {
               //Toast
               Toast.makeText(getBaseContext(),
                     getResources().getString(R.string.fehler),
                           Toast.LENGTH_LONG).show();
            }
         }
      });
       
        //delete Notification
        NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancelAll();
    }

}
FNORD this line does not exist!
-------------------------------------------------
T-Mobile G1 with Cyanogenmod 5.0.8 DS
HTC Desire S with Android 2.3.3
User avatar
LStrike
Junior Developer
Junior Developer
 
Posts: 20
Joined: Mon Feb 22, 2010 2:22 pm
Location: Hamburg, Germany

Top

Re: Launch by Default

Postby hasitharandika » Mon Jun 14, 2010 12:21 pm

hi LStrike,

i have a issue regarding this code.in my application it always displays the 1st message.

even in the notification it displays the current message but when displaying the message it displays the 1st message.

do you have any idea why is that??

this is where i set the notification

Code: Select all
      NotificationManager manager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
      String mobile = msg[0].trim();
      String sms_txt = msg[1].trim();
      String notificationText = msg[1].trim();
      Notification notification = new Notification(R.drawable.coloridicon,
            notificationText, System.currentTimeMillis());
      // ---forward the new SMS message---
      if (isCEDContact(mobile)) {
         listeneNativeInbox();
         Intent sms = new Intent(context, SaveReceivedSMS.class);
         sms.putExtra("MSG", msg);
         sms.putExtra("From_Noti", Constants.isFromNotify);
         sms.putExtra("notificationNr", NOTIFICATION_ID);
         sms.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         PendingIntent startIntent = PendingIntent.getActivity(context, 0,
               sms, 0);
         notification.setLatestEventInfo(context, "CED:", notificationText,
               startIntent);
         // notification.vibrate = new long[] { 100, 250 };
         notification.vibrate = new long[] { 100, 250 };
         manager.notify(NOTIFICATION_ID, notification);         
      }
      finish();


this is where i display the messgage

Code: Select all
Bundle bundle = getIntent().getExtras();
      String[] receivedSMS= bundle.getStringArray("MSG");
      isFromNotify = bundle.getBoolean("From_Noti");
      NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      nm.cancelAll();   



in this code when i'm retrieving the message it always holds the first message details


regards,
Randika
hasitharandika
Developer
Developer
 
Posts: 45
Joined: Mon Feb 15, 2010 8:37 am
Location: Sri Lanka

Re: Launch by Default

Postby LStrike » Mon Jun 14, 2010 3:34 pm

I have the same problem and have no solution yet.
FNORD this line does not exist!
-------------------------------------------------
T-Mobile G1 with Cyanogenmod 5.0.8 DS
HTC Desire S with Android 2.3.3
User avatar
LStrike
Junior Developer
Junior Developer
 
Posts: 20
Joined: Mon Feb 22, 2010 2:22 pm
Location: Hamburg, Germany

Re: Launch by Default

Postby hasitharandika » Tue Jun 15, 2010 7:49 am

hi LStrike,

i found the answer,

the issue is with the PendingIntent



Code: Select all
   Bundle bund = new Bundle();
         bund.putStringArray("MSG", msg);
         bund.putBoolean("From_Noti", Constants.isFromNotify);
         bund.putInt("notificationNr", NOTIFICATION_ID);

   PendingIntent startIntent = PendingIntent.getActivity(context, 0,
               sms, Intent.FLAG_ACTIVITY_NEW_TASK);


try it out. it works for me

regards,
Mike
hasitharandika
Developer
Developer
 
Posts: 45
Joined: Mon Feb 15, 2010 8:37 am
Location: Sri Lanka

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests